Re: The more I read about MVC, the more confused I get...
Re: The more I read about MVC, the more confused I get...
- Subject: Re: The more I read about MVC, the more confused I get...
- From: Bill Cheeseman <email@hidden>
- Date: Thu, 12 Jun 2003 21:08:41 -0400
You've got it almost exactly right, in my view.
As to how "the communication goes the other way around, from the model to
the views," yes, the model should post a notification when its data changes.
The controller (or anybody else) can register to observe this notification
and update the view accordingly. This is the canonical way in which Cocoa
separates the model from the view.
My only quibble is your suggestion that the model should post its entire
self in the notification, so that the controller can use the model's
accessor methods to update items in the view. This would be a breach of the
basic principle that the model should not know anything about the view, and
vice versa.
It would be much better if the model posted a notification for each specific
change in the data, and passed the specific new data item in the userInfo
parameter of the notification. Then the controller will know what the
specific new piece of data is, and can pass that specific piece of data --
and not the entire model -- along to the view. In this way, the separation
of model and view is maintained, and you can plug the model into a
completely different view in some future incarnation of your application,
without breaking anything.
on 03-06-12 5:54 PM, Koen van der Drift at email@hidden wrote:
>
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?
--
Bill Cheeseman - email@hidden
Quechee Software, Quechee, Vermont, USA
http://www.quecheesoftware.com
The AppleScript Sourcebook -
http://www.AppleScriptSourcebook.com
Vermont Recipes -
http://www.stepwise.com/Articles/VermontRecipes
_______________________________________________
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.