0
votes

I am using UITableViewController in my applicaton. While pop from tableview controller to back, i was setting nil to table view to deallocate the view controller from memory. its again calling viewDidLoad, please explain me why it is happening.

-(void)handleBackButton{
        self.tableView=nil;
        [self.navigationController popViewControllerAnimated:YES];
}
1
what? show us some code of allLithu T.V
I am overriding the back button event of UINavigationController and also setting self.tableView=nil, in that method. at that time it's happening.Kondamoori
Without code it is hard to help you. One thing: When you set self.tableView=nil, you are deallocation the table view, not the table view controller.Reinhard Männer
There's no need to set the table view to nil. When you pop the table view controller off the stack, it and its views will be deallocated.rdelmar
Please find my updated edit. Sorry for my typing mistake.Kondamoori

1 Answers

1
votes

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;
    }
}