开发者

interface builder setters? please punch me!

if you have an IBOutlet on an ivar like

IBOutlet UIView *view;

@property (nonatomic, retain) UIView *view;

that object created by ib开发者_C百科 will be managed by ib,

but what if you have,

UIView *view;

@property (nonatomic, retain) IBOutlet UIView *view;

does ib now use your setter to set that object? that would mean the setter has added +1 and needs to be set to nil or the object would leak?


IBOutlet doesn't do anything in the resulting code — it's literally erased by the preprocessor. It's just there so Interface Builder can scan your header to see which things it should treat as outlets.


Have a read here, a posting by Aaron Hillegass about some of this.

On the desktop, when a nib file is loaded, outlets are set in a sensible way: to set an outlet called foo, the nib loader looks for an accessor called setFoo:. If it is unable to find the accessor, the nib loader sets the variable foo directly. This sounds like key-value coding, right? It isn’t. The important difference is that nib loading treats foo as a weak reference; the object it points to is not retained.

Thus, if you create a subclass of NSViewController that has a dozen outlets to subviews, only the top-level view is retained. So, when the view controller is deallocated, it releases the top-level view and all the subviews are automatically deallocated. Tidy!

On the phone, however, the nib loader uses key-value coding to set the outlets; By default, outlets are treated as strong references. If you don’t have an accessor for your outlet, the view it refers to is retained.


It both cases you must release the outlet. If you have a property IB will use it and let you manage the retain or not (if you use assign). If you do not have a property IB will assign the value but retain it automatically, which you need to then release.

If you mark the property as an IBOutlet you don't need to also mark the class variable as an IBOutlet.


AFAIK, it doesn't matter if you put the IBOutlet on the ivar or the property. Either way, in general, IBOutlet properties should be (nonatomic, assign), and not retained, as the NIB loader handles all of that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜