site_archiver@lists.apple.com Delivered-To: cocoa-dev@lists.apple.com Le 1 juin 2010 à 06:53, Quincey Morris a écrit :
You've created another scenario by making your view need the managed object context (probably during 'drawRect:' at least?).…
Richard, this really points to an "abuse" of the MVC design pattern :-) If you look at some of the Cocoa controls, like NSTableView for example, you will see that there is also an NSTableViewDelegate protocol, with methods like -tableView:willDisplayCell:forTableColumn:row:, which have to be implemented, usually, in a Controller class. This ensures separation between the View and the Model it is meant to be representing. Using a delegate to obtain the data required for drawing a View means that the view has no need to know anything about the Model, the MOC or anything else, apart from the fact that something, somewhere, will supply the necessary information in response to a request via the delegate methods. So, for something simple like a CircleView, should also require a protocol like this: @protocol CircleViewDelegate - (void) circleView:(CircleView *)circleView willDisplayCircle:(Circle **)circle; @end This then is called from within the CircleView like this: - (void) someMethod; // in CircleView that needs the Circle coordinates { Circle *circle = nil; [delegate circleView:self willDisplayCircle:&circle]; // draw circle using circle.location and circle.radius. } Then the delegate method, in the Controller: - (void) circleView:(CircleView *)circleView willDisplayCircle:(Circle **)circle { NSPoint circleLocation = … get location from object in MOC CGFloat circleradius = … get radius from object in MOC circle = [Circle circleAtLocation:circleLocation withRadiusOf:circleRadius]; } Of course, this is a very simplistic (and not very well written) example, but you should get the idea that any code that talks to the MOC can be located in the Controller and accessed from the View via a delegate. Joanna -- Joanna Carter Carter Consulting -- Joanna Carter Carter Consulting _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/site_archiver%40lists.apple... This email sent to site_archiver@lists.apple.com