Reputation: 563
When one creates a UIViewController
and the corresponding nib file, this nib file usually comes with the same name as the controller, but it can be renamed. So, how does IB know which UIViewController
this nib file refers to?
When one drags from a referencing outlet or from an action, IB only shows the methods for a particular Controller. How is it chosen? When one renames the nib, I wonder whether IB saves the connection between the nib and the UIViewController
.
Does it seem like there is too much confusion in my head? :) Hope not...
Regards, Fak
Upvotes: 2
Views: 4769
Reputation: 576
I know this is an old ask. Just share my understanding here.
Let's say we create a subclass of UIViewController in XCode , and name it as SecondViewController whose xib filename will be SecondViewController.xib by default. Then let's see how to create this VC firstly, and then discuss what the File's Owner is.
Create and show SecondViewController with nib name: let's create the VC using initWithNibName:bundle: with the xib name specified, the code can be like this:
SecondViewController * vc = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[self presentViewController:vc animated:YES completion:nil];
Create and show SecondViewController without nib name: if everything is by default, that means the VC has the same class name as the xib file name, then we can ignore the nib name, the codes creating the vc can be like this:
SecondViewController * vc = [[SecondViewtController alloc] init];
[self presentViewController:vc animated:YES completion:nil];
Create and show SecondViewController with a different xib file name: if you rename the xib file name, then you must specify the xib file name when create the VC, codes can be like this:
SecondViewController * vc = [[SecondViewController alloc] initWithNibName:@"SecondViewController2" bundle:nil];
[self presentViewController:vc animated:YES completion:nil];
What is File's Owner? Firstly, as said by Apple, it's a placeholder object. This object is a container of the objects defined in the xib file. Also, this object can receive the messages sent by the objects in the xib, for example, the button click message. When you create an outlet or an action, their target will be the File's Owner, but this object is unknown in design time. In interface builder, it's shown like this:
What is File's Owner? Secondly, it's a class name shown in the interface builder. The instance of this class contains the objects defined in the xib. It is shown like this:
What if I don't set the class name for the File's Owner? In this case, IB will not allow you to create an outlet or action by control-drag, because it doesn't knows who will contain the outlet or the action.
What if I remove the class name for the File's Owner after I create the outlet or action? It's OK to remove the class name in IB. You creating codes will run fine as before.
Let's come back to the question: "How a nib file know's its File's Owner Controller"?
Upvotes: 1
Reputation: 11145
If you open an IB and check for file owner's class in forth tab in inspector window then here you can see the class name.
Upvotes: 3
Reputation:
A nib file doesn’t necessarily need to know which particular subclass of UIViewController
is its file’s owner. Note that view controller-owned nib loading is (usually) done in two steps:
UIViewController
is instantiated-initWithNibName:bundle:
, thus being aware of the nib file it is supposed to load, upon which the instance becomes the nib file’s owner.So it is the UIViewController
(or a subclass of it) instance that needs to know the nib file name, but the converse is not necessarily true.
That said, a subclass of UIViewController
normally defines outlets to objects in the nib file and receives actions. In order for the outlets and actions to be connected in Xcode/Interface Builder, you set the file’s owner class in the identity inspector to the corresponding subclass of UIViewController
.
Upvotes: 3