问题
How to reset a uinavigationview to display the root controller when user clicks back to it in a tab bar app
Hey,
Just wondering how I would do this. I have the navcontroller in my delegate along with the tabbar controller and Any time the user clicks to another tab I want the rootview on the navigation controller to be shown if and when they click back the the tab that contains the uinavcontroller.
Does this make sense?
Nick
回答1:
[self.navigationController popToRootViewControllerAnimated:YES];
Or NO if you don't want it to animate.
This way all the views that were cached are still there, i.e. you don't "remove/release" all the views above the root view, unless the navigationController deems it necessary.
I hope this was what You were looking for..
回答2:
place the code in appdelegate.m
if ([viewController isKindOfClass:[UINavigationController class]]) {
UINavigationController *nav = (UINavigationController *)viewController;
[nav popToRootViewControllerAnimated:NO];
}
回答3:
When using the UITabBar delegate method, you must delay the popToRootViewControllerAnimated call.
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
if ([self.selectedViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController *view=(UINavigationController *)self.selectedViewController;
[view performSelector:@selector(popToRootViewControllerAnimated:) withObject:nil afterDelay:.5];
}
}
来源:https://stackoverflow.com/questions/2336907/how-to-reset-a-uinavigationview-to-display-the-root-controller-when-user-clicks