Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing data from modal segue to parent

Tags:

ios

swift

segue

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 doneClicked(sender: AnyObject) {
    self.dismissViewControllerAnimated(true, completion: nil)
}

I can’t use segue.destinationViewController here to pass data as i’m used to do on push segues.

like image 263
Vasily Avatar asked Feb 13 '15 15:02

Vasily


Video Answer


1 Answers

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)
}
like image 141
Shashi3456643 Avatar answered Sep 29 '22 12:09

Shashi3456643