我想将数据(例如设置变量)从模态转场传递给父级,我该怎么做?
我正在使用该代码退出模态转场:
@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/