iosswiftswift4nstabviewcontroller

How to do a performSegue to a specific view in Tab Bar Controller from another view (swift 4)


In my iOS app, in a viewController I try to open a specific view in Tab Bar Controller.
I use a performSegue.
First I try to navigate straight to the specific view , but in this case the tab bar disappear
So I try to navigate to the tabViewController, and this lead me to the defult view (the first)

any idea how to navigate to a specific view in TabBarController ?

the performSegue I use:

self.performSegue(withIdentifier: "goToMain", sender: self)
//"goToMain" is my indentipier to the storyboard Segue

I use swift 4


Solution

  • with this code, you don't need segues, you can use when you push some button

    let VC1 = self.storyboard!.instantiateViewController(withIdentifier: "tabBarController") as! tabBarLoginViewController
    VC1.selectedIndex = 2 //this line says that the view that appears will be third of you tab bar controller
    self.navigationController!.pushViewController(VC1, animated: true)
    

    if you want to use segue use this, the segue needs to point of tab bar controller, not a view of tab bar controller

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
        if (segue.identifier == "goToMain") {
        let vc = segue.destination as! TabBarController
        vc.selectedIndex = 2 
        }
    }