简体   繁体   中英

Segue from ViewController to first tab of TabBarController

I've created a tabbed application. In the first tab (ViewController+TableView), it shows results taken from DB. I've a button that opens another ViewController in which user can filter results (for "city", for example).

Now, How can I return to the First tabbed page with a segue? Because I need to pass parameters in order to do a query to DB. If I create a segue between this page and the first ViewController it doesn't show tabbed menu.

Thank you guys.

I assume that what are you trying to achieve is to pass data from one tab to another.

"Now, How can I return to the First tabbed page with a segue? Because I need to pass parameters in order to do a query to DB. "

Actually, you don't have to perform a segue to pass the data, instead you can do the following:

// somewhere in your code when you need to pass data to another tab viewController:
if let tabBarController = tabBarController {
    let secondTabViewController = tabBarController.viewControllers![1] as! SecondViewController
    // let's assume that you want to pass a string
    secondTabViewController.str = "my str!!"
}

SecondViewController:

class SecondViewController: UIViewController {
    // here is the string that had been passed from the first tab viewController:
    var str:String?

    override func viewDidLoad() {
        super.viewDidLoad()

        // note that the output is: "Optional("my str!!")"
        print(str)

        // do optional binding:
        if let unwrappedStr = str {
            // output is: "my str!!"
            print(unwrappedStr)
        }
    }
}

I hope it helped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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