JPC
JPC

Reputation: 8276

NSWindow is nil after awakeFromNib

I have a nib file which has a bunch of views and custom objects in it. One of those objects is a custom controller object. In it's awakeFromNib method I want to access the window that is holding all of this stuff. I'm not sure how to get the window at this point. However, this custom object has an outlet to a view. I know that you can get the window from calling [nameOfView window] but for some reason, at this point, nil is being returned for the window, even though the view is non nil. At what point will the window be non nil?

As a side note, if I pass the window as a parameter to this custom object that is loaded from a nib file, do I have to worry about releasing/retaining it? How will memory management work with this NSWindow in my object that was loaded from a nib?

Upvotes: 1

Views: 934

Answers (1)

user557219
user557219

Reputation:

If I understood you correctly, you won’t be able to do this during the nib loading process:

  1. Some controller (let’s call it the master controller) triggers the load of that nib file;
  2. awakeFromNib is sent to the objects inside that nib file, including your custom controller object;
  3. The master controller gets a reference to nameOfView (potentially via the custom controller) and adds it to the view hierarchy of a window.

Loading a nib file containing views doesn’t automatically add those views to the view hierarchy of a window, which is why [nameOfView window] returns nil your custom controller’s awakeFromNib implementation. Since in step 1 there’s a master controller who’s responsible for triggering the load of that nib file, I assume this master controller is the one responsible for adding views to a window. Have the master controller send a message to the custom controller when this happens so that your custom controller knows when nameOfView has been added to a window.

Upvotes: 2

Related Questions