I’m a bit confused as to what happens exactly in Nib-based UIViewControllers. When generating a UIViewController subclass, the template contains a very specific comment in the viewDidUnload method:
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
To which subviews does this apply?
- The ones I initialized in
viewDidLoad
? (I’d say yes) - The ones I initialized in
initWithNibName
? (I’d say no) - The IBOutlets that reference objects in the Nib?
If I use the view controller like this:
MyViewController *controller = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
[controller release];
In this case I suppose it doesn’t matter much whether instance variables that hold references to subview are released in viewDidUnload
or dealloc
since dealloc
should get called as soon as the view controller is popped off the stack so I might as well do as Apple says and release instance variables on viewDidUnload
instead of dealloc
.
But suppose I used MyViewController
as an instance variable that may be pushed multiple times:
if(self.myViewController == nil) {
self.myViewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
}
//Change some state which should be reflected in the view
self.myViewController.someProperty = someValue;
[self.navigationController pushViewController:self.myViewController animated:YES];
What happens in MyViewController
if I release an IBOutlet in viewDidUnload? Can I count on having a new reference to it on the next viewDidLoad
?
In other words: what happens to the view itself after viewDidUnload
? Is it released and re-loaded from the Nib if the controller is pushed again? Or does the view remain in memory? And if so, do the outlets get re-set before viewDidLoad
?
If either the view remains in memory and the outlets are re-set before viewDidLoad
or is reloaded each time the controller is pushed, I suppose it would be correct to release the outlets in viewDidUnload
(even though in the first case it does not matter). But otherwise (specifically if the view remains in memory and the outlets are NOT re-set), releasing the subviews in viewDidUnload
is wrong for the use case I presented, am I correct?