How to create Unwind segue programmatically

2019-07-12 19:22发布

I make an application using no storyboard and in this part of my app I need to create a unwind segue from ThirdViewController to FirstViewController programmatically only. I know how to do it using sotorybard but can't find any solution how to do it programmatically. Any sugestions?

1条回答
Viruses.
2楼-- · 2019-07-12 19:51

You can't create an actual segue without a Storyboard, but you can do the equivalent action by calling popToViewController on navigationController. Here is an example that is connected to a button to return to FirstViewController from ThirdViewController:

@IBAction func goBackToFirstVC(sender: UIButton) {
    guard let controllers = navigationController?.viewControllers else { return }
    let count = controllers.count
    if count > 2 {
        // Third from the last is the viewController we want
        if let firstVC = controllers[count - 3] as? FirstViewController {
            // pass back some data
            firstVC.someProperty = someData
            firstVC.someOtherProperty = moreData

            navigationController?.popToViewController(firstVC, animated: true)
        }
    }
}

In an override of viewWillAppear, you can act upon that data that was passed back.

查看更多
登录 后发表回答