如何通过popViewControllerAnimated为Swift发回数据? [英] How to send data back by popViewControllerAnimated for Swift?
本文介绍了如何通过popViewControllerAnimated为Swift发回数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要通过popView将一些数据从secondView发送回First View。
如何通过popViewControllerAnimated发回数据?
I need to send some data back from secondView to First View by popView. How can i send back the data by popViewControllerAnimated?
谢谢!
推荐答案
您可以使用委托传递数据
- 创建
协议
inChildViewController
- 创建
委托
变量ChildViewController
- 在<$中扩展
ChildViewController
协议c $ c> MainViewController - 提供对
MainViewController的
当ChildViewController
的引用导航
- 定义
委托
方法MainViewController
- 然后你可以从
ChildViewController调用
委托
方法
- Create
protocol
inChildViewController
- Create
delegate
variable inChildViewController
- Extend
ChildViewController
protocol inMainViewController
- Give reference to
ChildViewController
ofMainViewController
whennavigate
- Define
delegate
Method inMainViewController
- Then you can call
delegate
method fromChildViewController
示例
在ChildViewController中:在下面编写代码...
In ChildViewController: Write code below...
protocol ChildViewControllerDelegate
{
func childViewControllerResponse(parameter)
}
class ChildViewController:UIViewController
{
var delegate: ChildViewControllerDelegate?
....
}
在MainViewController中
// extend `delegate`
class MainViewController:UIViewController,ChildViewControllerDelegate
{
// Define Delegate Method
func childViewControllerResponse(parameter)
{
.... // self.parameter = parameter
}
}
**Segue**
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
let goNext = segue.destinationViewController as ChildViewController
goNext.delegate = self
}
没有Segue
let goNext = storyboard?.instantiateViewControllerWithIdentifier("childView") as ChildViewController
goNext.delegate = self
self.navigationController?.pushViewController(goNext, animated: true)
方法调用
self.delegate?.childViewControllerResponse(parameter)
这篇关于如何通过popViewControllerAnimated为Swift发回数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文