Outlets / IBOutlet declarations (was Re: Interface Builder & Wiring Objects)
Outlets / IBOutlet declarations (was Re: Interface Builder & Wiring Objects)
- Subject: Outlets / IBOutlet declarations (was Re: Interface Builder & Wiring Objects)
- From: mmalcolm crawford <email@hidden>
- Date: Mon, 17 Nov 2008 18:11:29 -0800
On Nov 17, 2008, at 3:06 PM, mmalcolm crawford wrote:
Going forward, you're encouraged to declare outlets as follows:
@interface AppController : NSObject {
NSTextField *myTextField;
...
}
@property (nonatomic, retain) IBOutlet NSTextField *myTextField;
Jeff asked (posted with permission):
"One remaining question. Are you expected to release the outlet when
your
controller is dealloc'd? And did you have to prior to "properties"?
I believed the answer to the latter was "no" but I'm prepared to be
wrong on that.."
Since the property is declared with the 'retain' attribute, the memory
management semantics are made clear here. Yes, you should release the
outlet in dealloc.
The problem heretofor was that the story was a mess -- whether or not
you released the outlet was dependent upon what class File's Owner
inherited from, and whether or not you had accessor methods. The
principal advantage with the property-based pattern is that memory
management is consistent across all classes across all patterns. It
will also work on modern runtimes that use instance variable synthesis
(where there may be no instance variable declaration with which to
attach the IBOutlet).
There may be some variation; if you have a delegate that may be
connected using a delgate, then you may have:
@interface AppController : NSObject {
id delegate;
...
}
@property (nonatomic, assign) IBOutlet id delegate;
although again use of a property declaration makes the memory
management semantics clear (in this case you would of course not
release delegate in dealloc).
One other consideration, particularly in iPhone applications, is where
you might have outlets to subviews of a main view that might be
released in sime situations -- e.g. a UIViewController whose view is
released in didReceiveMemoryWarning. To ensure that you don't prolong
the lifetime of objects you got from the nib, you should set (use your
accessor methods to) set those variables to nil in the relevant
method, e.g.:
@interface MyController : UIViewController {
UILabel *label;
...
}
@property (nonatomic, retain) IBOutlet UILabel *label;
then
- (void)didReceiveMemoryWarning {
self.label = nil;
[super didReceiveMemoryWarning];
}
mmalc
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden