ios - 将数据从模态转场传递给父级

标签 ios swift segue

我想将数据(例如设置变量)从模态转场传递给父级,我该怎么做?

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

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

我不能在这里使用 segue.destinationViewController 来传递数据,就像我以前在推送 segues 上所做的那样。

最佳答案

在 Modal ViewController 上创建协议(protocol)

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

同时在你的 Modal ViewController 类中声明

var delegate:ModalViewControllerDelegate!

在 ParentViewController 中包含此协议(protocol) ModalViewControllerDelegate

当您从一个 View Controller 移动到另一个 View Controller 时

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

这里你在ParentViewcontroller中获取你的值

 func sendValue(value: NSString) {

    }

最后在 ModalViewController 上

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

关于ios - 将数据从模态转场传递给父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28502653/

相关文章:

ios - Instagram API 登录错误 - 此页面无法加载

ios - UIView 什么时候知道它有多大?

swift - 在 Swift 的 UIKit Dynamics 中,如何定义一个圆边界来包含一个 UIView?

swift - 用户可以在不同帐户上恢复相同的购买

ios - 无法推送 viewController,因为 navigationController 为 nil

ios - 单击按钮时 UITextField 不会结束编辑(委托(delegate) textFieldDidEndEditing)

ios - UIImageView 纵横比?

multithreading - performSelectorOnMainThread 与 performSegue

ios - 调用 didSelectRowAtIndexPath 时字符串变为空白

ios - 协议(protocol)中 segue 标识符的条件