Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What problems may cause setting nib file's owner to nil?

If I can load an object from a nib file without using file's owner then what is the reason of existence of file's owner? Also the outlet-action connections can be created without the use of file's owner. For example i can make the connections directly from the object to the nib. So again i really can't understand the need for file's owner. Does it have any relation with MVC pattern? Does file's owner must be of UIViewController type?

like image 339
Marios Mourelatos Avatar asked Jun 04 '12 13:06

Marios Mourelatos


1 Answers

During the loading of a nib file, Cocoa generates each object serialized in the nib file. Then, for each connection in the nib file, it calls setValue:forKey: on the target object to create the connection. Some connections are to the object nil. Those setValue:forKey: messages are sent to whatever object is passed as the file owner.

If you have no file owner, then the nil connections will be ignored. If you have no nil connections, then it would be no different than not having a file owner. This is not particularly common.

All of this allows you to instantiate multiple instances of the same nib file objects, by passing different file owners to to the loading process.


EDIT:

Remember, a nib file is just a bunch of serialized objects. When you programmatically create a view controller with initWithNibName:bundle:, the view controller already exists before the nib file is loaded. The objects inside the nib file almost always want to be able to refer to that view controller. So we pass the view controller to UINib as the file owner. Anywhere the nib file says nil, UINib replaces that with the file owner (typically the view controller).

This is very flexible, but flexibility isn't the point. There'd be no way to refer to the view controller inside the nib file if you didn't pass it in during nib instantiation.

like image 95
Rob Napier Avatar answered Oct 11 '22 17:10

Rob Napier