Unwind from a VC to a TabBarController and change tab index

。_饼干妹妹 提交于 2019-12-31 05:40:15

问题


I have the following 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.


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/56032435/unwind-from-a-vc-to-a-tabbarcontroller-and-change-tab-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!