How are people popping their UINavigationController stacks under a UITabBarController?

我与影子孤独终老i 提交于 2019-12-08 03:29:40

问题


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


来源:https://stackoverflow.com/questions/2506771/how-are-people-popping-their-uinavigationcontroller-stacks-under-a-uitabbarcontr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!