views:

248

answers:

4

if you have an IBOutlet on an ivar like

IBOutlet UIView *view;

@property (nonatomic, retain) UIView *view;

that object created by ib 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?

+2  A: 

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.

epatel
i kind of thought that, but that still doesnt answer if its any different to put the outlet on the ivar or the @property itself. it SEEMS like it doesnt make a difference.
drunknbass
the actual `IBOutlet` is defined as an empty macro and is only used by InterfaceBuilder to find outlets. Try ctrl-click `IBOutlet` in Xcode and `Jump to Definition`
epatel
The corollary to that is that while the nib loader won't retain the object for you, if you declare the property as retaining, then when the nib loader calls the synthesized setter, the setter *will* retain the object.
Peter Hosey
A: 

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.

Kendall Helmstetter Gelner
ok, finally makes sense after reading that post.. so basically as soon as you connect an outlet(thats not explicitly assign) that class now puts a +1 on the object..My original and i guess naive thinking was if i create an object in ib, its memory management was automatic. and was released for me when the controller was released. but that seems to only be the case if you use objects in ib and never connect them to anything, as soon as you connect to an outlet you(object) now are responsible for it.
drunknbass
I think you are responsible for any instances that you actually are using... So if you're given a reference to a View, you should release that view because it was automatically retained when itwas sent to you but not released when it comes back? or maybe you should retain when you get it?...
Brian Postow
@drunkbass: You are essentially correct, although one bit of magic is taken care of for you - when using a view controller, the view is wired to the view controller "view" property and so that is taken care of for you by the view controller. @Brian: if you have an IBOutlet, you have to release it UNLESS you use an assign property. That works because usually IBOutlets you use are already subviews of views, which means the containing view is retaining them and thus the outlet will be retained until the view goes away. If they are not in a view I think an assign property would not work.
Kendall Helmstetter Gelner
+3  A: 

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.

Chuck
A: 

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.

Shawn Craver