Let's suppose to have a tab bar controller with two tabs, A and B, where A is a navigation controller.
When the user is in A, he can push A1 and then A2, which are both view controllers. A back button on A2, performs:
[self.navigationController popViewControllerAnimated:YES];
which correctly triggers the dealloc method on A2.
If the user is in A2 and then switches to tab B, I need the dealloc method to be called on A2; therefore I've implemented the following method in the TabBarController:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
UINavigationController *nc = (UINavigationController*)tabBarController.selectedViewController;
[nc popToRootViewControllerAnimated:YES];
return YES;
}
but in this flow the dealloc method of A2 is never called! How is it possible that popping from A2 to A1 it works, whereas changing tabs the view controller is not deallocated?
Thanks for any hint!
DAN
It seems that
didSelectViewController
is triggered before the tab controller has switched to the destination tab. Therefore the execution of the methodpopToRootViewControllerAnimated
correctly pops all the view controllers, but can't really release the visible one, as it is currently being depicted by the app.To achieve my result, I've figured out the following solution. In the
viewDidAppear
method of all the view controllers which correspond to the first view controller for each tab, I executepopToRootViewControllerAnimated
(all of them are navigation controller).