Pop to root view when tab is selected
Solution 1
Following delegate is called while each tab is selected on tabbar.
-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
Put following code inside this delegate method.
if ([viewController isKindOfClass:[UINavigationController class]])
{
[(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
}
its working fine on my app.
Solution 2
For Swift lovers:
import UIKit
class YourTabBarControllerHere: UITabBarController,
UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self;
}
func tabBarController(tabBarController: UITabBarController,
didSelectViewController viewController: UIViewController) {
if let vc = viewController as? UINavigationController {
vc.popViewControllerAnimated(animated: false);
}
}
}
Edit: Swift 3 update, thanks to @Justin Oroz for pointing that out.
Solution 3
In Swift 3.1
Add UITabBarControllerDelegate to your TabBar Class:
class YourClass: UITabBarController, UITabBarControllerDelegate {
After:
override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
let yourView = self.viewControllers![self.selectedIndex] as! UINavigationController yourView .popToRootViewControllerAnimated(false)
}
Solution 4
What you are trying to do sounds a little bit odd. Have you read the Human Interface Guidelines on combining UINavigationControllers and UITabBarControllers?
However, what you need to do is detect the selection of the tab by setting a delegate for your UITabBarController and implementing the tabBarController:didSelectViewController: delegate method. In this method you need to pop back to the root view controller using UINavigationController's popToRootViewControllerAnimated: method.
Solution 5
[self.navigationController popToRootViewControllerAnimated:NO];
John S
Updated on July 05, 2022Comments
-
John S almost 2 years
I've been having some trouble with something I thought might be easy. I have a table in my root view controller, when a row is selected I push a new view and from there I go to another tab.
My question is how do I make sure that as soon as the user taps the first tab the navigation controller will pop to root?
-
Steve Moser over 10 yearsIf you check out Apple's Phone.app the favorites and recent tab's behavior is the same as what the OP wants.
-
Robin Summerhill over 10 yearsThe Phone app does not switch tabs when you select a table row - this was the behaviour I was querying, not popping to root when the user taps a different tab.
-
Steve Moser over 10 yearsAh, yes the OP's question is not entirely what OP means by "from there I go to another tab". Does the user select another tab or does the app switch automatically.
-
ravinder521986 over 10 yearsReally very helpful for me, working fine in my app according to my requirement.
-
Glogo over 9 yearsChanging animated to NO actually helped me. When I had animated set to YES, my tab bar nor any other buttons in navigation controller were not working.
-
nwkeeley over 9 yearsWhat file is the delegate? I tried creating a UINavigationController and specifying it as the delegate. I see its called on viewDidLoad but specifying it as the delegate for UITabBarDelegate or UITabBarControllerDelegate doesn't call this method.
-
Yatendra about 8 yearsI am bit confused.Can you tell me where I add this code
-
Justin Oroz almost 7 yearsAs OP's question asked for ROOT controller, it should be (in Swift 3 syntax)
vc.popToRootViewController(animated: false)
-
Gellie Ann almost 7 yearsFor those who can't make this doesn't work, try adding
UITabBarControllerDelegate
in the header file (.h), andself.tabBarController.delegate = self;
in the implementation file (.m). -
Stewart Lynch over 5 yearsThis approach logs Unbalanced calls to begin/end appearance transitions for the targeted ViewController even though it does work. I am trying to find a solution that will not log the error.