Create your subviews in -viewDidLoad
. If you need ivars for them then only assign their values. The reference is hold by adding the views as subviews to you main view.
Then when your view is unloaded you should set your ivars to nil, because the object have been released since your view was removed and released.
So in your header
@interface MyViewController : UIViewController {
IBOutlet UIView *someSubview; // assigned
}
@property (nonatomic, assign) IBOutlet UIView someSubview;
@end
And in your implementation
@implementation MyViewController
//... some important stuff
- (void)viewDidLoad;
{
[super viewDidLoad];
someSubview = [[UIView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:someSubview]; // retains someSubview
[someSubview release]; // we don't hold it
}
- (void)viewDidUnload;
{
[super viewDidUnload];
someSubview = nil; // set the pointer to nil because someSubview has been released
}
//... more important stuff
@end
If you wish you can also not release someSubview
in -viewDidLoad
, but then you have to release it in -viewDidUnload
AND -dealloc
since (if I remember right) -viewDidUnload
isn't called before -dealloc
. But this isn't necessary if you don't retain someSubview
.