release view in viewDidUnload
I know that if I retain an IBOutlet by using property then I have to set it to nil in viewDidUnload
but what about others?
For example, i have three subviews view1, view2 and view3, that load from nib and that is the controller's header file
@interface MyViewController : UIViewController {
IBOutlet UIView *view1;
UIView *view2;
//no reference for view3
}
@property (nonatomic, retain) IBOutlet UIView *view2; //property view2 is an IBOutlet!!
@end
and method viewDidUnload
- (void)viewDidUnload {
self.view2 = nil;
//[view1 release];
//view1 = nil;
[super viewDidUnload];
}
do I have to release view1 and set it to nil? or UIViewController will set it to nil for me? what about view3?
开发者_C百科also do I have to release view1 in dealloc
?
edit: I think many people does not understand my question
Firstly, view1 is an IBOutlet which declared as an ivar and assign an ivar will not retain it. I know that UIViewController definitely will retain it but do i have to release it or UIViewController will release it for me? If UIViewController will release it then there is no point that i have to release it again.
Secondly, view2 is also an IBOutlet although it is declared as a property not ivar.
@property (nonatomic, retain) IBOutlet UIView *view2;
It is a retain property, therefore set it will retain it so I know that I have to set it to nil in order to release it. I have no problem about it.
For view3, there is no reference for it, therefore I am assuming I don't have to do anything about it. I also assuming there is no need to make a reference for every
object in nib.
All outlets are retained by default even if they don't have a property declared for them. So you will need to release them. If you go on to declare an assign
ed property as an outlet, then you don't need to release but you can't rely on it either as you are not an owner.
So you need to release both view1
and view2
as they are declared as outlets. view3
is a variable that doesn't exist and hence needn't be worried about.
When a nib is loaded, all its objects are automatically instantiated and retained. The file's owner of your nib file is then the owner of your UIView.
If you use UIView *view2 you can't connect them using interface builder. So that doesn't make really sense to me.
You have to release in dealloc as well.
- (void)viewDidUnload {
self.view1 = nil;
self.view2 = nil;
[super viewDidUnload];
}
- (void)dealloc {
[self.view1 release];
self.view1 = nil;
[self.view2 release];
self.view2 = nil;
}
精彩评论