I have an iPhone app that has four tabs. On Tab 1 there is a UINavigationController. Users can go three levels deep on the stack of views.
Let's say a user goes to level 2 of the navigation Tab 1, and then switches to Tab 3. Is there any way to make a button that a user can press that will then popToRoot that nav controller on Tab 1 (behind the scenes) while they are still on Tab 3? That way when the touch Tab 1 again they will be at root level?
I don't want it to be at root level EVERY time they touch Tab 1, only when they touch that button on Tab 3 will it pop Tab 1 to root level.
You can send messages to any valid object as long as you have a reference to it. If your app delegate owns the UITabBarController and each view controller in the UITabBarController is a UINavigationController then anywhere in your app you can do the following:
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication].delegate;
UINavigationController *navController = [appDelegate.tabBarController.viewControllers objectAtIndex:indexOfNavController];
[navController popToRootViewController:NO]; // animation not needed but could be YES
In swift
language. Put this code in to AppDelegate
.
Add UITabBarControllerDelegate
to AppDelegate
func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController){
if let navController = viewController as? UINavigationController{
navController.popToRootViewControllerAnimated(false)
}
}