4

I have 5 tabs and when popping to the root controller it takes me to the last used tab. Is there a way I can jump to a specific tab?

 //takes me to last used tab on the tab controller
 @IBAction func goHome(sender: AnyObject)
{
 self.navigationController?.popToRootViewControllerAnimated(true)
}

for example, If I have 10 view controllers open and then click on the button above I want to jump to tabcontroller index 0 which is the home page

swiftTonio
  • 119
  • 1
  • 2
  • 6
  • If you are using a TabController with something like `self.tabBarController.selectedIndex = 1` to select the first tab,then you could get the nav controller for that specific tab and popToRootViewController. – Lucho Apr 01 '16 at 12:59
  • similar problem.. http://stackoverflow.com/questions/8963450/jump-to-a-specific-tab-in-a-uitabbarcontroller – Droid GEEK Apr 01 '16 at 13:00

5 Answers5

5
func goToRootOfTab(index: Int) {
    let window = (UIApplication.shared.delegate as? AppDelegate)?.window
    let tabBar :UITabBarController? =  window?.rootViewController as? UITabBarController
    tabBar?.selectedIndex = index
    // pop to root if navigated before
    if let nav = tabBar?.viewControllers?[index] as? UINavigationController {
        nav.popToRootViewController(animated: true)
    }
}
Bassant Ashraf
  • 1,531
  • 2
  • 16
  • 23
2

This code will take you to the tab and pop to the root view controller for that tab.

func buttonAction(sender: AnyObject){
    let someTabIndex = 0
    // Get the tabBar
    let t = self.tabBarController
    // Change the selected tab item to what you want
    t?.selectedIndex = someTabIndex
    // Pop the navigation controller of that index
    let v = t?.viewControllers?[someTabIndex]
    if let n = v?.navigationController {
        n.popToRootViewControllerAnimated(true)
    }

}
ryantxr
  • 4,119
  • 1
  • 11
  • 25
  • If you want to `popToRootViewController(animated: false)`, you have to hide this navigation controller first. So, `t?.selectedIndex = i` should be called earlier than pop, just as described in apple's doc about `popToRootViewController(animated:)` – DawnSong Dec 09 '16 at 11:26
0

Since this is Swift, you might want to check that you have a tab bar controller as an ancestor in case your app structure ever changes:

@IBAction func goHome(sender: UIButton) {
    self.navigationController?.popToRootViewControllerAnimated(true)
    if let tab = self.tabBarController {
        tab.selectedIndex = 0  // Or whichever number you like
    }
}
Phillip Mills
  • 30,888
  • 4
  • 42
  • 57
  • this still takes me to the previously used tab. The view controller im popping from does not have the tab controller displayed, which is why im attempting to pop to root and then select the tab but it still doesn't work =( – swiftTonio Apr 01 '16 at 13:26
  • If your current controller isn't part of the tab controller's structure, how did it get displayed? (Or, maybe the question should be: how did its navigation controller get displayed?) The answers people have posted are all reasonable for a normal tab/navigation structure; what's different with this one? – Phillip Mills Apr 01 '16 at 13:33
  • im on a navigation controller which pops to root to a tab controller but I'm unable to select the index after popping to root. I'm not sure what's different with mine compared to other peoples – swiftTonio Apr 01 '16 at 16:59
  • But it sounds as if the navigation controller isn't inside a tab. How does the navigation controller -- or the navigation controller's root controller -- get displayed? – Phillip Mills Apr 01 '16 at 17:32
0

First Case:When you want to select other tab index

guard let VCS = self.navigationController?.viewControllers else {return }
for controller in VCS {
    if controller.isKind(of: TabBarController.self) {
        let tabVC = controller as! TabBarController
        tabVC.selectedIndex = index . (Select any index for tab)
        self.navigationController?.popToRootViewController(animated: true)
    }
}

Second Case: When you want to access to RootViewController variables

guard let VCS = self.navigationController?.viewControllers else {return }
for controller in VCS {
    if controller.isKind(of: TabBarController.self) {
        let tabVC = controller as! TabBarController
        //    tabVC.selectedIndex = 0 . //no need of this line if you want to access same tab where you have started your navigation
        let VCs = tabVC.selectedViewController as! MyViewController
        VCs.variableName = true . //access your variable
        self.navigationController?.popToRootViewController(animated: true)
    }
}
Gurpreet Singh
  • 803
  • 9
  • 16
0

First get reference to selected tab and then pop to root.

if let tab = tabBarController  {
     tab.selectedIndex = 0
     if let navcon = tab.selectedViewController as? UINavigationController {
                        navcon.popToRootViewController(animated: true)
     }

}
Avijit Nagare
  • 8,482
  • 7
  • 39
  • 68