Unwind from a VC to a TabBarController and change tab index

前端 未结 2 426
孤城傲影
孤城傲影 2021-01-27 17:04

I have the following Storyboard layout:

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

In the HomeView I have a but

相关标签:
2条回答
  • 2021-01-27 17:31

    To go from TestView to HistoryView, should be something along these lines:

    // From TestView to HomeView(0)
    navigationController?.popViewController(animated: true)
    // Change the active tab in the tabcontroller
    (navigationController?.presentingViewController as? UITabBarController)?.selectedIndex = 1
    
    0 讨论(0)
  • 2021-01-27 17:46

    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.

    0 讨论(0)
提交回复
热议问题