如何通过popViewControllerAnimated为Swift发回数据?
我需要通过popView将一些数据从secondView发送回First View。 我如何通过popViewControllerAnimated发回数据?
谢谢!
您可以使用delegate
传回数据
- 在
ChildViewController
创建protocol
- 在
ChildViewController
创建delegate
变量 - 在
MainViewController
扩展ChildViewController
协议 -
navigate
时引用MainViewController
ChildViewController
- 在
MainViewController
定义delegate
方法 - 然后,您可以从
ChildViewController
调用delegate
方法
例
在ChildViewController中 :写下面的代码……
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 }
没有塞格
let goNext = storyboard?.instantiateViewControllerWithIdentifier("childView") as ChildViewController goNext.delegate = self self.navigationController?.pushViewController(goNext, animated: true)
方法调用
self.delegate?.childViewControllerResponse(parameter)
如果你想通过弹出发送数据,你可以这样做:
func goToFirstViewController() { let a = self.navigationController.viewControllers[0] as A a.data = "data" self.navigationController.popToRootViewControllerAnimated(true) }