Return to the Main Index
Return to the Alphabetic Index

Copyright (c) 1994 by NeXT Computer, Inc. All Rights Reserved.


(informal protocol)

Category Of: NSObject

Declared In: AppKit/NSNibLoading.h

Protocol Description

This informal protocol consists of a single method, awakeFromNib. It's implemented to receive a notification message that's sent after objects have been loaded from an Interface Builder archive.

When loadNibFile:owner: or a related method loads an Interface Builder archive into an application, each custom object from the archive is first initialized with an init message (initFrame: if the object is a kind of View). Outlets are initialized via any setVariable: methods that are available (where variable is the name of an instance variable). (These methods are optional; the Objective C run time system automatically initializes outlets.) Finally, after all the objects are fully initialized, they each receive an awakeFromNib message.

The order in which objects are loaded from the archive is not guaranteed. Therefore, it's possible for a setVariable: message to be sent to an object before its companion objects have been unarchived. For this reason, setVariable: methods should not send messages to other objects in the archive. However, messages to other objects can safely be sent from within awakeFromNibby this point it's assured that all the objects are unarchived and fully initialized.

Typically, awakeFromNib is implemented for only one object in the archive, the controlling or owner object for the other objects that are archived with it. For example, suppose that a nib file contained two Views that must be positioned relative to each other at run time. Trying to position them when either one of the Views is initialized (in a setVariable: method) might fail, since the other View might not be unarchived and initialized yet. However, it can be done in an awakeFromNib method:

- awakeFromNib


NXRect viewFrame;

[firstView getFrame:&viewFrame];

[secondView moveTo:viewFrame.origin.x + someVariable


return self;


There's no default awakeFromNib method; an awakeFromNib message is only sent if an object implements it. The Application Kit declares a prototype for this method, but doesn't implement it.

Notification of Loading

- (void)awakeFromNib Implemented to prepare an object for service after it has been loaded from an Interface Builder archivea so-called nib file. An awakeFromNib message is sent to each object loaded from the archive, but only if it can respond to the message, and only after all the objects in the archive have been loaded and initialized. When an object receives an awakeFromNib message, it's already guaranteed to have all its outlet instance variables set. There's no default awakeFromNib method.