Search code examples
iosswiftuitabbarcontrollersegueunwind-segue

Unwind from a VC to a TabBarController and change tab index


I have the following Storyboard layout:

diagram of Storyboard layout

When the app starts it goes to HomeView VC at TabIndex 1.

In the HomeView I have a button and I segue to the TestView using

performSegue(withIdentifier: "goToStartTest", sender: self)

Now I want to go from TestView to HistoryView. The only way I can see to do that right now is to create a segue from TestView to TabBarController. This brings up HomeView with Tab Bar intact. I do this from TestView using

performSegue(withIdentifier: "testToRes", sender: self)

However, that leaves me on HomeView, I need HistoryView.

The research I've done points to using Unwind and detecting where segue came from and acting accordingly.

I'm using the latest Xcode with Swift 5.


Solution

  • Set up your HomeViewController like this:

    class HomeViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view.
        }
    
        var goToHistory = false
    
        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
            if goToHistory {
                goToHistory = false
    
                // Use whichever index represents the HistoryViewController
                self.tabBarController?.selectedIndex = 1
            }
        }
    
        @IBAction func unwindAndGoToHistory(_ segue: UIStoryboardSegue) {
            goToHistory = true
        }
    }
    

    In your TestViewController, either:

    1. Wire a UIButton to the Exit icon to unwind to unwindAndGoToHistory:.

      OR

    2. Create a programmatic unwind by wiring from the View Controller icon to the Exit icon (again selecting unwindAndGoToHistory:), and then find this segue in the Document Outline view and give it an identifier such as "unwindToHistory" in the Attributes Inspector. When you're ready to segue, trigger it with performSegue(withIdentifier: "unwindToHistory", sender: self).

    Then, when the unwind segue is triggered, iOS will pop the TestViewController and call unwindAndGoToHistory(). In there, goToHistory gets set to true. Afterwards, viewWillAppear() will be triggered. Since goToHistory is now true, the code will set the selectedIndex of the tabBarController to switch to the HistoryViewController's tab.

    Note: This will show the unwind animation back to HomeViewController, then the view will automatically switch to HistoryViewController. If instead you'd like to switch to HistoryViewController immediately with no animation, override viewWillAppear instead of viewDidAppear.