swiftTonio
swiftTonio

Reputation: 119

Pop to root controller but to specific tab on the tab controller

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

Upvotes: 4

Views: 5575

Answers (5)

Avijit Nagare
Avijit Nagare

Reputation: 8802

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)
     }

}

Upvotes: 0

Gurpreet Singh
Gurpreet Singh

Reputation: 953

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)
    }
}

Upvotes: 0

Bassant Ashraf
Bassant Ashraf

Reputation: 1607

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)
    }
}

Upvotes: 5

ryantxr
ryantxr

Reputation: 4217

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)
    }

}

Upvotes: 2

Phillip Mills
Phillip Mills

Reputation: 31026

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
    }
}

Upvotes: 1

Related Questions