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 comb开发者_运维知识库ination 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.
yes you can:
@interface SomeClass : UIViewController {
IBOutlet UILabel *myLabel;
}
@end
@property is only for defining memory management and getter and setter, read-write ability etc.
There are two reasons to use property for views:
- Easier/safer memory managment (Really good reason)
- Exposing your interface to other clasess (usually bad coding practice)
I always use properties just to make sure that my code doesn't crash if I forget to nil the IBOutlets in viewDidUnload. After all it isn't that hard to just write 2 lines of code.
If you would like to save some typing don't declare ivars at all. The code below works just fine:
@interface MyController : UIController {
}
@property (nonatomic, retain) IBOutlet UIImageView* myImage;
@end
@implementation MyController
@synthesize myImage;
- (void) dealloc()
{
self.myImage = nil;
[super dealloc];
}
- (void) viewDidUnload()
{
self.myImage = nil;
[super viewDidUnload];
}
@end
If you're not planning on manipulating or accessing information about your views from other view controllers or objects and planning on using them internally (in owning view controller) you don't need @property
statments at all. Just define them as IBOutlet
instance variables in your view controller's interface.
Interface Builder or the NIB loader doesn’t depend on the properties to be there. If the connected variables can be accessed via KVC the nib loader can make the connections.
But you really should consider using synthesized properties instead of hand-written accessors. This is much less error-prone because it doesn’t require as much boilerplate code. The less code you have to write the better.
精彩评论