1
votes

I'm using the tab bar controller template that is packaged with swift. I have embedded the FirstViewController and SecondViewController into a navigation controller. I have added a third ViewController that is accessed by a segue from the FirstViewController. When I'm in the third ViewController and I hit the SecondViewController tab the simulator takes me to the SecondViewController but when I hit the FirstViewController tab, I'm taken back to the third ViewController - I want to go back to the FirstViewController when I select it's tab. What is the way to implement this? Searching through the documentation I think I should be using:

    func tabBarController(_ tabBarController: UITabBarController,
     shouldSelectViewController viewController: UIViewController) -> Bool
2

2 Answers

1
votes

Pop to root view when tab is selected. This answer seems relevant.

Swift code:

func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) -> Bool {
   if let viewController = viewController as? UINavigationController {
      viewController.popToRootViewController(animated: false)
   }
}
0
votes

For newer Versions of Swift the delegate function should be as following:

func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
    if let viewController = viewController as? UINavigationController {
        viewController.popToRootViewControllerAnimated(false)
    }
}

Your class has to be consistent with the UITabBarControllerDelegate protocol and the delegate property should be set during the loading process.