How does nib-loading code establish connections to outlets after instantiating objects?
Apple documentation says:
Once all objects have been instantiated and initialized from the archive, the nib loading code attempts to reestablish the connections between each object’s outlets and the corresponding target objects. If your custom objects have outlets, an NSNib object attempts to reestablish any connections you created in Interface Builder. It starts by trying to establish the connections using your object’s own methods first. For each outlet that needs a connection, the NSNib object looks for a method of the form setOutletName: in your object. If that method exists, the NSNib object calls it, passing the target object as a parameter. ...
But how does it determines the target object?
For example I have an object
@interface Foo: NSObject
{
IBOutlet NSButton *redButton;
}
...
@end
and the redButton connected with the button on the window through IB. How will nib-loading code establish the connection?
Is there any "connection data" in the nib file?
Yes, of course the NIB file has to store these connections. If you open a XIB file in a text editor, you will find snippets like this for every outlet connection:
<object class="IBConnectionRecord">
<object class="IBOutletConnection" key="connection">
<string key="label">launchAtLoginCheckBox</string>
<reference key="source" ref="1001"/>
<reference key="destination" ref="951818764"/>
</object>
<int key="connectionID">72</int>
</object>
So it looks like Apple uses instances of the private classes IBConnectionRecord
and IBOutletConnection
to model the connections.