Re: Advise on referencing NSDocument from custom view
Re: Advise on referencing NSDocument from custom view
- Subject: Re: Advise on referencing NSDocument from custom view
- From: Quincey Morris <email@hidden>
- Date: Sun, 10 Aug 2014 10:15:58 -0700
On Aug 10, 2014, at 09:01 , Luc Van Bogaert <email@hidden> wrote:
> I was thinking of using a weak property for my custom view to reference the document and set this property from the same windowDidLoad: method, but I'm not sure if this is a good approach to take. Any advise on this?
It’s not a bad approach, but it’s probably not necessary, since the view can already get the document as ‘self.window.windowController.document’.
In a simple project, putting a reference to the document from a view is fine, but in a larger project I’d be a bit reluctant. The “problem” is that it puts some implementation details (that your data model is implemented in a subclass of NSDocument) in a place where they’re really irrelevant.
A better approach is to define your data model separately from the document, even if the document is responsible for creating and owning the data model. The window controller would vend references to the data model (not the document) to views and view controllers that need it.
In an even more sophisticated project, this may still over-constrain your design. Another approach is to create an auxiliary data model in the window controller, that’s specific to the needs of the custom view. The window controller is then responsible (usually via KVO observations) for keeping the auxiliary model in sync with the main data model.
The point here is to reduce the amount of information that’s globally known, because you’ll find (in later design iterations) that global knowledge will frustrate your attempts to rearrange your design as you add features, or come to understand the design problem better.
_______________________________________________
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