How to create Unwind segue programmatically

我与影子孤独终老i 提交于 2019-12-11 02:24:48

问题


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:


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.



来源:https://stackoverflow.com/questions/38162535/how-to-create-unwind-segue-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!