开发者

In cocoa, how do you connect the model to viewcontrollers?

So from what I understand the basic application structure for cocoa programs is an AppController in MainMenu.xib. The AppController then initializes the WindowControllers and ViewControllers which has all the UI 开发者_StackOverflow中文版logic and in turn load the xib files for the rest of the application. The AppController also creates the Model classes.

My confusion is over how to get the data from the model classes into the views. I would like to use bindings and key-value observing. Should each view controller have a pointer to the AppController and the ModelController? If so how would you do that?

[[ViewController alloc] initWithModel:ModelController];

Is the only solution I can think of and it doesnt seem very clean.


I make a controller that creates/loads and owns the model. My application delegate creates and owns this controller.

That same controller usually also owns the window or view; I rarely use dedicated window controllers, and have never used view controllers. If you do use window/view controllers, the model controller would create and own those, too.

Thus, one controller is responsible for both the model (or some specific portion of it) and the (controller for the) window that displays that (portion of the) model.


A common way to link a NSViewController to a model is to set its representedObject value. There is no need to set a pointer to the AppController, as it can always be obtained through [NSApplication sharedApplication].delegate.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜