简体   繁体   English

Swift-如何使用从ViewController到特定TabBarController的按钮进行搜索

[英]Swift - How do I segue with a button from ViewController to a specific TabBarController

From a ViewController before a TabBarController in Swift, I would like to send to a specific Tab after the TabBarController that may be a NavigationController or a simple ViewController. 我想从Swift中的TabBarController之前的ViewController发送到TabBarController之后的特定Tab,它可以是NavigationController或简单的ViewController。

For the moment I make like this: 就目前而言,我是这样的:

@IBAction func settingsButtonPressed(sender: AnyObject) {
        tabBarController?.selectedIndex = 3 // 4d tab
        performSegueWithIdentifier("tabBarShow", sender: self)
}

With this action, it performs the segue but it always show the first tab. 通过此操作,它执行segue,但始终显示第一个选项卡。

Thanks for the help! 谢谢您的帮助!

You can use: 您可以使用:

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if(segue.identifier == "tabBarShow"){
        if let tabVC = segue.destinationViewController as? UITabBarController{
            tabVC.selectedIndex = 3
        }
    }
}

You can try set the selectedViewController property instead of the selectedIndex : 您可以尝试设置selectedViewController属性而不是selectedIndex

tabBarController.selectedViewController = tabBarController.viewControllers![2]

This person had the same problem: https://stackoverflow.com/a/17919837/3933375 这个人有同样的问题: https : //stackoverflow.com/a/17919837/3933375

EDIT This is on the assumption that the tabBarController has been initialised, if not you'll need to initialise it first. 编辑这是基于tabBarController已被初始化的假设,如果没有初始化,则需要首先对其进行初始化。

You are setting the selected index before the TabbarController is loaded , so store your selected index somewhere , and set this in TabBarViewController's ViewDidLoad. 您正在加载TabbarController之前设置选定的索引,因此将选定的索引存储在某个位置,然后在TabBarViewController的ViewDidLoad中进行设置。

self.selectedIndex = valueOFSelectedIndexStored; self.selectedIndex = valueOFSelectedIndexStored;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM