In my UIViewController subclass, I have 3 UIView‘s with each a @property as an IBOutlet. I do not use these properties at all in my code. The views get instantiated as soon as the view controller is created and they are deallocated when the view controller is deallocated.
I was thinking; can’t I just remove the @property‘s? I did, and I could still connect my instance variables (with IBOutlet) in Interface Builder.
So my question now is; is there any use for properties in combination with Interface Builder, or is it OK to leave them out? Is it required for some memory management or something? Or are they really just for use in your own code?
And if I do leave them out, do I still need to release them in dealloc?
They’re not necessary, but they’re strongly encouraged for the simple fact that they clarify memory management.
For example, if you declare an outlet via the ivar, then what is its retain policy? Is it retained? Is it autoreleased? Are you the owner? Does someone else also own it? There is a lot of ambiguity there (especially with those accursed top level objects, which behave differently on the Mac than on the iPhone).
On the other hand, if you declare the outlet via a property, there is no ambiguity, because the memory management policy is directly stated in the declaration. In addition with the presence of the property, the nib unarchiving will see the setter and use that, thereby ensuring that nothing strange is going on with transferring object ownership, etc.
In a nutshell, you can declare outlets without using
@property(which we all had to do before they were introduced in 10.5), but there’s no good reason to not use them. They really make the code a lot clearer as to what exactly is going on.For more info on the absurdity of nib object memory management, check out this page in the documentation.