Hope Hope - 11 days ago 6
iOS Question

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.

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.

Thanks for the help!

Answer

You can use:

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