Pop to root view when tab is selected

21,869

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];
Share:
21,869
John S
Author by

John S

Updated on July 05, 2022

Comments

  • John S
    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
    Steve Moser over 10 years
    If you check out Apple's Phone.app the favorites and recent tab's behavior is the same as what the OP wants.
  • Robin Summerhill
    Robin Summerhill over 10 years
    The 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
    Steve Moser over 10 years
    Ah, 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
    ravinder521986 over 10 years
    Really very helpful for me, working fine in my app according to my requirement.
  • Glogo
    Glogo over 9 years
    Changing 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
    nwkeeley over 9 years
    What 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
    Yatendra about 8 years
    I am bit confused.Can you tell me where I add this code
  • Justin Oroz
    Justin Oroz almost 7 years
    As OP's question asked for ROOT controller, it should be (in Swift 3 syntax) vc.popToRootViewController(animated: false)
  • Gellie Ann
    Gellie Ann almost 7 years
    For those who can't make this doesn't work, try adding UITabBarControllerDelegate in the header file (.h), and self.tabBarController.delegate = self; in the implementation file (.m).
  • Stewart Lynch
    Stewart Lynch over 5 years
    This 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.