Re: MVC and Cocoa question
Re: MVC and Cocoa question
- Subject: Re: MVC and Cocoa question
- From: Graham Cox <email@hidden>
- Date: Tue, 11 Nov 2008 10:34:58 +1100
On 11 Nov 2008, at 3:56 am, Steven Guitar wrote:
i am working on the challenge exercises for aaron hillegass' book,
3rd edition, for chapter 18 challenge. i have setup the application
with a subclass of NSDocument (MyDocument), a subclass of NSView
(DrawPadView), and a model component called Shape.
basically as the user uses the mouse to draw shapes, i have an
NSMutableArray in DrawPadView that contains the various shapes drawn
by the user.
when it comes time to save this file, MyDocument is going to do all
the work, but the only way i can think of to get an NSMutableArray
in MyDocument to have the values of the array in the DrawPadView is
to use the NSNotificationCenter.
is this ok? should i have my NSDocument class register as an
observer of the NSView class? i was not sure if this was going
against MVC or any other cocoa design patterns.
It sure does go against MVC.
DrawPadView is a View. NSDocument is part of the model. The reason you
are having trouble is that a) your model data is in the wrong place
and b) you have no C (Controller).
If you put your mutable array in your document subclass, it becomes
easy to archive for saving purposes. Then, your view can ask the
document for the array when it needs to draw the existing shapes.
Ideally you would interpose a controller between the two to act as a
go-between (since this is a fairly simple situation, you could do it
without a controller, but for the purposes of learning the MVC
approach, it might be better to go with it). The controller can (to
help make it earn its keep) also handle the creation of new shapes.
You can instantiate the controller in your document nib, and hook it
to the document and view using outlets. There's no reason to use
notifications to 'synchronise' the data, just make sure the data is in
the right place, and not duplicated - then it can't help but always be
up to date.
hth,
Graham
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden