Re: I can't supply document reference to (init) with bindings, can I?
Re: I can't supply document reference to (init) with bindings, can I?
- Subject: Re: I can't supply document reference to (init) with bindings, can I?
- From: Graham Cox <email@hidden>
- Date: Mon, 30 Nov 2009 11:54:18 +1100
On 30/11/2009, at 10:31 AM, David Hirsch wrote:
> I'm trying to use bindings as much as possible, but I think I need to do this with actions & outlets. I have a NSArrayController (courseController) managing an NSMutableArray (courses) of Course objects. In order to correctly initialize each Course, I need to have data from other arrays (like the rooms array) that my document knows about. In (init), I tried to use the sharedDocumentController to get the current document, but in reading online, that seems to be unwise (what if the window or application is not frontmost), and furthermore, it seems not to work: I cannot get a reference to the current document from the sharedDocumentController, for some reason.
>
> So: is there some tricky way to have the "add new course" button tell the courseController to add a new Course, but somehow call a custom init (like init:withDocument:) and supply some reference to the current document? I doubt it, but it seems worth asking.
>
> Thanks in advance.
> -Dave
It sounds like an architectural muddle.
A 'document' is part of the data model, but the idea of the 'current document' is really dependent on the document associated with the active window, which is a view. A data model in and of itself should not be concerned with which window is active, since that violates MVC.
What does each document represent? Typically a document will represent one example of the main 'thing' that your app deals with, and so often owns or embodies an instance of the data model. If parts of that data model need the document they belong to, then you can easily arrange a way for them to get it, without relying on which document is current, which is likely to set up unwanted dependencies and strange behaviours that will be hard to control.
Work out your architecture properly before worrying about some of the implementation details, like what you can call from an -init method. A clean, clear architecture usually makes the implementation obvious.
--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