从VC展开到TabBarController并更改选项卡索引 [英] Unwind from a VC to a TabBarController and change tab index

查看:133
本文介绍了从VC展开到TabBarController并更改选项卡索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下情节提要布局:

I have the following Storyboard layout:

应用启动时,它会转到TabIndex 1处的HomeView VC.

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

HomeView中,我有一个按钮,然后使用

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

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

现在我想从TestView转到HistoryView.我现在看到的唯一方法是创建一个从TestViewTabBarController的序列.这样会弹出HomeView,而标签栏保持完整.我从TestView使用

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)

但是,这使我只能使用HomeView,我需要HistoryView.

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

我所做的研究指向使用Unwind并检测segue的来源并采取相应的措施.

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

我在Swift 5中使用最新的Xcode.

I'm using the latest Xcode with Swift 5.

推荐答案

像这样设置您的HomeViewController:

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

在您的TestViewController中,或者:

  1. UIButton连接到Exit图标以展开到unwindAndGoToHistory:.

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

OR

通过将View Controller图标连接到Exit图标(再次选择unwindAndGoToHistory:)来创建程序化展开,然后在 Document Outline 视图中找到此序列,然后在 Attributes Inspector 中为其指定一个标识符,例如"unwindToHistory".准备好进行搜寻时,请按performSegue(withIdentifier: "unwindToHistory", sender: self)触发它.

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

然后,当触发取消同步时,iOS将弹出TestViewController并调用unwindAndGoToHistory().在那里,goToHistory设置为true.之后,将触发viewWillAppear().由于goToHistory现在是true,因此代码将设置tabBarControllerselectedIndex切换到HistoryViewController的选项卡.

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.

注意::这会将展开动画显示回HomeViewController,然后视图将自动切换到HistoryViewController.如果 相反,您想立即切换到HistoryViewController,而无需 动画,请覆盖viewWillAppear而不是viewDidAppear.

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.

这篇关于从VC展开到TabBarController并更改选项卡索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆