Re: View Confusions
Re: View Confusions
- Subject: Re: View Confusions
- From: Quincey Morris <email@hidden>
- Date: Thu, 27 Oct 2016 01:32:43 -0700
- Feedback-id: 167118m:167118agrif8a:167118sfrNvrEJHN:SMTPCORP
On Oct 26, 2016, at 20:15 , Kevin W Pitcher <email@hidden> wrote:
>
> NSDocument loads fine but in the NSWindowController windowDidLoad self.document is NULL
> Where and when can I get at my document data to pass it onto the NSTabViewController and it’s NSViewController for Binding?
One possibility that springs to mind:
If your window controller init (assuming you have one) refers to self.window, that causes the window to be loaded immediately. I would imagine that windowDidLoad would then be called before the init returned, and at that time it doesn’t seem feasible for the “document" property to be set yet.
One way of doing an end-run around such order-of-initialization issues is to use KVO. Let’s say your document subclass has a “model” property that is needed by a view controller. If you configure your view controller to observe key path “self.windowController.document.model”**, you will get notified when any property on that path gets modified. Then, so long as you write your view controller to expect that the model is nil initially (either because it’s really nil, or because something earlier in the path hasn’t been set yet and is still nil), then you can defer your view controller setup until the model is available, without having to know the exact timing.
This is also advantageous if your document’s model property can change for other reasons (e.g. if you handle document reverting by simply re-creating the model from the original document file).
(For all of this to work, you need to make sure your model and other relevant custom properties are KVO compliant.)
Note that you have some flexibility about who does the observing, and what path to use. You can have each view controller observe, or have the window observe and inject the model reference into relevant view controllers, and so forth. The details depend on what other references and relationships you have between the controllers.
** Unfortunately, view controllers don’t know their window controller by default, so you’re going to have to arrange for the window controller reference to be injected separately. The only path from a view controller to the document that is defined by default is this monstrosity:
self.view.window.windowController.document.model
which is fine, but isn’t going to give you the model until the view is added to the window hierarchy, which is quite late (between viewWillMoveToSuperview and viewDidMoveToSuperview).
_______________________________________________
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