Better IB outlets in Objective-C 2.0

2008
12.15

Apple docs lack clarity when it comes to whether IBOutlets retain objects they point to. However a new way to specify outlets in Obj-C 2.0 saves the day.

@interface MyController : NSObject
{
    NSTextField* _text;
}
@property (readwrite, retain) IBOutlet NSTextField* text;
@end

@implementation MyController
    @synthesize text=_text;
@end

This mechanism allows a developer to control retention behavior association with an outlet. Why should you care? The story with loading a nib (xib) file is that top-level objects are returned retained and it is left up to a developer to release them when they are no longer needed. Misunderstanding of this detail results in numerous leaks across applications.

A way to avoid tracking the top-level objects is to structure nibs so that every top-level object is reachable from the “File’s Owner” and to release top-level objects upon loading. However, this strategy brings about sad results unless one ensures that IBOutlets are always retained.

Your Reply

You must be logged in to post a comment.