Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa Interface Builder object initialization

Base on the documentation and sample code that I have gone through, I got an impression that when a class defined in xcode is read into and configured in Interface Builder, an object based on the class is effectively created and stored in an xib or nib file. So the object is ready to be used when the corresponding application is launched.

Alternatively, for classes that have not been handled by Interface Builder, code such as the "new" statements have to be written in xcode explicitly in order for the associated objects to be created and used.

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

like image 484
Stanley Avatar asked Jan 15 '11 04:01

Stanley


People also ask

How do you initialize an object in Objective C?

Out of the box in Objective-C you can initialize an instance of a class by calling alloc and init on it. // Creating an instance of Party Party *party = [[Party alloc] init]; Alloc allocates memory for the instance, and init gives it's instance variables it's initial values.

What is awake from nib?

The nib-loading infrastructure sends an awakeFromNib message to each object recreated from a nib archive, but only after all the objects in the archive have been loaded and initialized. When an object receives an awakeFromNib message, it is guaranteed to have all its outlet and action connections already established.


1 Answers

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

For each IB object 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 a custom object that you instantiate through Interface Builder (drag a generic Object to your class list, then in the Identity tab of the inspector, set the custom object to be an instance of your class). Then, right-click on your custom object in Interface Builder. You should see an entry for your IBOutlet in the window that pops up. Drag from the little circle next to it to (in this example) your main window. You now have a reference to the IB object in Xcode.

It is through making these connections (with IBOutlets for references and IBActions for methods) that you define much of the behavior of your application.

like image 136
Ryan Ballantyne Avatar answered Sep 27 '22 17:09

Ryan Ballantyne