Objective-C/Cocoa: Proper design for delegates and controllers

前端 未结 4 2019
余生分开走
余生分开走 2020-12-28 23:11

Consider the following common situation:

You have some MainView in your Cocoa application, loaded from a NIB, which is controlled by a MainViewCon

4条回答
  •  悲&欢浪女
    2020-12-28 23:54

    Here are 2 options that come to mind:

    • If the event going to delegate is send by your controller, you can have that method return a value to the controller to push to the view.

    • You could also bind the infoLabel's value to some key (using, say, Cocoa bindings or just raw Key-Value Observing). The bound object (which could be the delegate, or some other model object) could just update the bound key, which would push a value to the infoLabel. As an example, you could bind the delegate's "info" member to the infoLabel's value. When the delegate receives an event, it can update the info member, and the view changes. The actual binding itself could happen in IB (if your delegate is in the nib) or in the controller (which has a reference to the view and the delegate.)

    The latter solution is basically a circular reference, but one which seems somehow cleaner to me.

提交回复
热议问题