Does an IBOutlet needs to be a property & synthesized?
On Mac OS X, IBOutlets are connected like this:
- Look for a method called set<OutletName>:. If it exists call it.
- If no method exists, look for an instance variable named <OutletName>, set it without retaining.
On iPhone OS, IBOutlets are connected like this:
- call [object setValue:outletValue forKey:@"<OutletName>"]
The behavior of set value for key is to do something like this:
- Look for a method called set<OutletName>:. If it exists call it.
- If no method exists, look for an instance variable named <OutletName>, set it and retain it.
If you use a property, you'll fall into the "Look for a method called set<OutletName>:..." case on both platforms. If you just use an instance variable, then you'll have different retain/release behavior on Mac OS X VS iPhone OS. There's nothing wrong with using an instance variable, you just need to deal with this difference in behavior as you switch between platforms.
Here's a link to full documentation on just this topic. https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/LoadingResources/CocoaNibs/CocoaNibs.html#//apple_ref/doc/uid/10000051i-CH4-SW6
On Mac OS X, IBOutlets are not retained by defaults. This is the opposite of the behavior on iPhone OS: on iPhone OS, if you don't declare a property it is retained and you must release this property in the dealloc
method. Additionally, the 64-bit runtime can synthesize instance variables using property declarations. That means that someday the instance variables (with the IBOutlet
) may be omitted.
For these reasons it is more homogeneous and compatible to create always a property and use the IBOutlet
only in the property. Unfortunately, it is also more verbose.
In your first example you always have to release the outlet in the dealloc
method. In your second example you must release the outlet only with iPhone OS.