The more I read about MVC, the more confused I get...
The more I read about MVC, the more confused I get...
- Subject: The more I read about MVC, the more confused I get...
- From: Koen van der Drift <email@hidden>
- Date: Thu, 12 Jun 2003 17:54:56 -0400
Hi,
Although I do understand the concepts of MVC, using it with Cocoa still
somehow confuses me. I do know how to set up all the connections in IB, and
how delegates, outlets and actions work, so that's not the problem. It's
the actual coding part that confuses me, especially the responsibilities
that classes should have and understanding how the model and the views
communicate with each other.
My app is a document based program with (so far) one main window. The
window contains a textview in which the user can edit a string that is the
heart of the model. The window also contains a table view and some text
items that display the results of some calculations on the model, and more
widgets to control the calculations.
I have read online articles, mailinglist discussions and tutorials, and
came to the conclusion that a 'model - modelcontroller - viewcontroller -
view' setup fits my app the best.
The way I understand it, the following applies:
MyDocument: this is the 'modelcontroller' that owns the model, and is
responsible for opening/saving. It is a subclass of NSDocument.
MyModel: the model that does all the calculations.
MyMainWindowController: the viewcontroller that responds to user actions in
the main window. A subclass of NSWindowController.
MyTextView, NSTableView, NSPopupButtons, etc: the views that form the GUI.
Now suppose the user types in MyTextView to edit the model.
MyMainWindowController is the delegate and will get a notification. Now how
do I pass this on to the model?
One possible way would be:
(void) textDidChange:(NSNotification *)aNotification
{
// pseudocode: get string from sender of notification
[[self document] setModelString:aString];
}
MyDocument can then pass the string to the model. Would this be
'recommended/best' way to do this?
Another problem is understanding how the communication goes the other way
around, from the model to the views
Again, suppose the model has changed after the user typed in MyTextView and
the other views need to be updated accordingly. The data that some of the
views display is derived from the model. However, the viewcontroller does
not know about the model. But how do I get the data from the model into the
different views?
One way I thought of is that MyDocument gets a notification from the model
when something has changed or has been calculated. MyDocument can then tell
MyMainWindowController to update all the views, and passes the model as an
argument so the views can use accessors to get the updated data.
Is that a usable approach?
But how does this work for the NSTableView? Can I set MyModel (which
provides an NSArray that is used to fill the table) to be the datasource of
the NSTableView?
Last question. My app also uses several NSDictionaries that are used to
setup the popup-buttons. The model uses data from the dictionary, depending
on the selection in the popup-button the user made. Which class would be
the most logical to own these NSDictionaries? At a later stage I want to
implement user defined entries for the dictionaries.
Comments and uggestions are as always very welcome,
- Koen.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.