I am keeping a reference to the example object but I never use retain, new, or copy.
@interface ExampleViewController : UIViewController
{
IBOutlet UILabel *example;
}
@end
If I connect a UILabel object in Interface Builder to example, the retain count is two. But when I don’t connect it, the retain count is zero. I would think that I am keeping one reference and the view is keeping another, therefore I would need to release it. What do you think?
IBOutletinstance variables are a special case. In iOS, each IBOutlet instance variable is retained when the NIB is loaded, so you do need to release them yourself.In fact, you should take special care to release and nil these IBOutlets in
-viewDidUnload:, otherwise they’ll leak when the NIB is reloaded.Note that this is actually different from Mac OS X development, where IBOutlets are not retained unless it’s a top-level NIB object without a parent view or window.
For this reason, Apple now recommends exposing IBOutlets via properties rather than instance variables. That way, the ownership of the outlet is clearly defined.
myButtonwould not have to be released;someObjectwould have to be released. And in both cases, you’re responsible for cleaning up these variables in-viewDidUnload:.