将数据从模态segue传递给父级 [英] Passing data from modal segue to parent

查看:83
本文介绍了将数据从模态segue传递给父级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数据(例如set var)从modal segue传递给parent,我该怎么做?

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

我正在使用该代码退出模态segue:

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

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

我不能使用 segue.destinationViewController 这里传递数据,因为我习惯于推送segues。

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

推荐答案

在Modal ViewController上创建协议

Create protocol on Modal ViewController

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

同样在你声明Modal ViewController类

Also declare in you Modal ViewController class

var delegate:ModalViewControllerDelegate!

在ParentViewController中包含此协议ModalViewControllerDelegate

Include this protocol ModalViewControllerDelegate in ParentViewController

当您将一个viewController移动到另一个时,

When you are Moving form one viewController to another

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

在这里你得到您在ParentViewcontroller中的值

Here you get your value in ParentViewcontroller

 func sendValue(value: NSString) {

    }

最后在ModalViewController上

Finally on ModalViewController

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

这篇关于将数据从模态segue传递给父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆