Passing data from modal segue to parent

后端 未结 2 568
暗喜
暗喜 2020-12-28 20:05

I want to pass data (e.g. set var) from modal segue to parent, how can I do that?

I’m using that code to exit from modal segue:

@IBAction func doneC         


        
2条回答
  •  一整个雨季
    2020-12-28 20:24

    In the second viewController (the one showed by the segue) declare a variable like

    var parentVC : UIViewController?
    

    then when you call segue from parent

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
    {
        if segue.identifier == "yourSegue" {
            let secondController= segue.destinationViewController as UIViewController
    secondController.parentVC = self
        }
    }
    

    so you can use

    @IBAction func doneClicked(sender: AnyObject) {
        self.parentVC.yourVariable = 0
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    

提交回复
热议问题