What happens if I load a nib w/o passing the file owner in the xib as the owner in -loadNibNamed: owner:

80 views Asked by At

I only just recently learned that the file owner to a xib does not have to be a NSWindowController or NSViewController (don't know why I thought that).

So I understand that this is the order things happen:

  1. An object is instantiated (w/ MVC, the controller). Outlet properties are not connected, and point to nil

  2. The nib has to be initialized, with -initWithNibNamed (and will be loaded when the view or window is accessed) or w/ -loadNibNamed (which will initialize then load the nib together)

  3. The outlets are all connected once the nib is initialized

  4. -awakeFromNib is called on the controller

But now what doesn't make sense to me, is why it even matters that I specify the owner of the nib in the -loadNibNamed call.

   BOOL effectsXibLoaded = [[NSBundle mainBundle] loadNibNamed:@"Effects"
                                                         owner:self.effectsController
                                               topLevelObjects:nil];

I say this because I imagine because we have specified in the nib who the file owner is-- and therefore when the nib is initialized, it should know who to connect it's outlets to right?

Or is specifying the owner in -loadNibNamed just a way to add another object to the owners list (if that's a thing?). I imagine there can only be one owner who receives the -awakeFromNib call from one nib, but I'm not sure.

In other words, if I specify the owner, when the nib is waking, it will notify said owner, as well as the owner specified in the xib?

Also I am not confident in my understanding of the order of how things execute, so if I am in need in corrections please don't hesistate

1

There are 1 answers

0
stevesliva On

The owner in the xib is a class.

The owner in loadNibNamed is an instance.

There's some magic in NSApplicationMain where it figures out the app delegate class, creates an instance, makes that the owner of the main nib, and sets that instance as the application's delegate... but there's nothing here to indicate a instance of the class specified as File's Owner will be generated if you don't alloc init one first...