< Previous PageNext Page >

Outlets

Object composition is a dynamic pattern that requires an object to somehow acquire references to its constituent objects so that it can send messages to them. It typically holds these other objects as instance variables. These variables must be initialized with the appropriate references at some point during the execution of the program.

An outlet is such an object instance variable, but it is special in that the reference is configured and archived through Interface Builder. The connections between the containing object and its outlets are reestablished every time the containing object is unarchived from its nib file. The containing object holds an outlet as an instance variable with the type qualifier of IBOutlet. For example:

@interface AppController : NSObject
{
    IBOutlet NSArray *keywords;
}

Because it is an instance variable, an outlet becomes part of an object’s encapsulated data. But an outlet is more than a simple instance variable. The connection between an object and its outlets is archived in a nib file; when the nib file is loaded, each connection is unarchived and retained, and is thus always available whenever it becomes necessary to send messages to the other object.

The type qualifier IBOutlet is a tag applied to the instance-variable declaration so that the Interface Builder application can, during development, synchronize the display and connection of outlets with Xcode. In other words, you can add and connect outlets for a custom object and then generate header files with the outlets defined for you. Or you can, in Xcode, declare outlets (using the IBOutlet qualifier) in a header file; Interface Builder can then pick up these new declarations so that you can connect them and store those connections in a nib file. Figure 5-1 shows how you connect an outlet in Interface Builder.


Figure 5-1  Connecting an outlet in Interface Builder

Connecting an outlet in Interface Builder

An application typically sets outlet connections between its custom controller objects and objects on the user interface, but they can be made between any objects that can be represented as instances in Interface Builders, even between two custom objects. As with any instance variable, you should be able to justify its inclusion in a class; the more instance variables an object has, the more memory it takes up. If there are other ways to obtain a reference to an object, such as finding it through its index position in a matrix, or through its inclusion as a function parameter, or through use of a tag (an assigned numeric identifier), you should do that instead.



< Previous PageNext Page >


© 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-12-20)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.