Passing data from modal segue to parent

后端 未结 2 592
暗喜
暗喜 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:51

    Create protocol on Modal ViewController

    protocol ModalViewControllerDelegate
    {
        func sendValue(var value : NSString)
    }
    

    Also declare in you Modal ViewController class

    var delegate:ModalViewControllerDelegate!
    

    Include this protocol ModalViewControllerDelegate in ParentViewController

    When you are Moving form one viewController to another

     modalVC.delegate=self;
            self.presentViewController(modalVC, animated: true, completion: nil)
    

    Here you get your value in ParentViewcontroller

     func sendValue(value: NSString) {
    
        }
    

    Finally on ModalViewController

    @IBAction func doneClicked(sender: AnyObject) {
    delegate?.sendValue("value")
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    

提交回复
热议问题