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?
Share
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 objectnil. ThosesetValue:forKey:messages are sent to whatever object is passed as the file owner.If you have no file owner, then the
nilconnections will be ignored. If you have nonilconnections, 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 toUINibas the file owner. Anywhere the nib file saysnil,UINibreplaces 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.