How are people popping their UINavigationControlle

2019-05-29 18:01发布

问题:

I have four UINavigationControllers assigned each to a tab in a UITabBarController. Each UINavigationController manages a UIViewController, which may itself branch out into other UIViewControllers below it hierarchally.

My question is, in a case in which a user, under one tab, has navigated to a UIViewController that hierarchally BELOW the main UIViewController managed by the UINavigationController, and then the user pushes a different tab, and then goes back to the original tab, HOW can I make it so that the user is presented with the main UIViewController managed by the UINavigation controller? and not the page where he left off?

UITabBarController is set up in IB

回答1:

Implement the UITabBarControllerDelegate protocol and pop to the root controller whenever your delegate is notified that the user has selected a different tab.

Something like:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    [viewController.navigationController popToRootViewControllerAnimated:YES];
}

The code above should pop to the root controller of any navigation controller that is the main view controller of the newly selected tab. You could try implementing the protocol in your application delegate. And do not forget to actually assign the app delegate as the tab bar controller's delegate.



回答2:

didn't work for me originally, then later on I noticed that all my viecontrollers for different tabs are actually UINavigationControllers

and thus I modified the above code a little bit as follows and it worked:

[(UINavigationController *)viewController popToRootViewControllerAnimated:NO];