Cocoa Initializing an Interface Builder Object

Based on the documentation and sample code I went through, I got the impression that when a class defined in xcode is read and configured in Interface Builder, an object based on the class is effectively created and stored in xib or a nib file. Thus, the object is ready for use when starting the corresponding application.

Alternatively, for classes that have not been processed by the Builder interface, code such as the "new" operators must be explicitly written in xcode so that related and related objects are created and used.

It will be very nice to have people who are more knowledgeable than me to confirm or correct my very naive understanding of the Builder interface ...

+4
source share
2 answers

Your understanding is correct, but incomplete. Yes, Interface Builder instantiates classes and serializes them into NIB. However, these objects are not automatically available for your code.

For each IB object that you want to access through Xcode, you need to declare an IBOutlet variable. Example:

IBOutlet NSWindow* mainWindow; // A Reference to your main window 

Put this code in the header file of the user object that you create through the Builder interface (drag the general object into the list of classes, and then on the Identification tab of the inspector set the special object as an instance of your class). Then right-click on your custom object in Interface Builder. You should see the entry for your IBOutlet in the window that appears. Drag from the small circle next to it in (in this example) the main window. You now have a reference to the IB object in Xcode.

It is thanks to these connections (with IBOutlets for links and IBActions for methods) that you determine most of the behavior of your application.

+6
source

Nib files contain an archived copy of the object graph; everything an NSNib or NSBundle needs to know in order to create new instances of all objects stored in the nib file joined together as described using nib bindings and with their properties set to the values ​​specified in nib.

The complete process is described in the Object Loading Process.

Objects are not created automatically from the tip, but are created on demand in response to the loading of any nib file. The difference is important because you can have one object loading a tip more than once or several objects load the same nib file.

For example, I could create anything whose “file owner” is a UIViewController that associates a single UIView object with the property property of the file owner. Then I could have two different subclasses of the UIViewController (or two instances of the same subclass of UIViewController), loading the same nib file so that each gets its own copy of the UIView. Similarly, I could load a new UITableViewCell from the knife every time I need a new cell for the table.

+1
source

Source: https://habr.com/ru/post/1335640/


All Articles