You could set a breakpoint in viewDidLoad
and look at the stack trace to see why it's reloading its view.
But I can make an educated guess. When you tell the navigation controller to pop a view controller, it needs to animate that view controller's view
off the screen. To do so, it probably asks the disappearing view controller for its view
.
A UITableViewController
's view
is the same as its tableView
. When you set tableView
to nil
, you also set view
to nil
. So when the navigation controller asks the table view controller for its view
(so it can animate the view off the screen), the table view controller notices that its view
is nil, so it loads its view. And to load its view, it sends itself loadView
and then it sends itself viewDidLoad
.
I don't know why you would really bother trying to unload the view. If the view controller itself gets deallocated, it will release its view (which will deallocate the view unless you've retained it somewhere else). And if the view controller doesn't get deallocated, isn't that usually because you might want to put its view back on the screen soon?
Anyway, if you really want to get rid of a view controller's view, don't set its view
to nil
while the view might still be in the on-screen view hierarchy. Wait until the view is definitely out of the hierarchy. For example, subclass UITableViewController
, and override didMoveToParentViewController:
like this:
- (void)didMoveToParentViewController:(UIViewController *)parentViewController {
[super didMoveToParentViewController:parentViewController];
if (parentViewController == nil) {
self.tableView = nil;
}
}