如何通过popViewControllerAnimated为Swift发回数据?

我需要通过popView将一些数据从secondView发送回First View。 我如何通过popViewControllerAnimated发回数据?

谢谢!

您可以使用delegate传回数据

  1. ChildViewController创建protocol
  2. ChildViewController创建delegate变量
  3. MainViewController扩展ChildViewController协议
  4. navigate时引用MainViewController ChildViewController
  5. MainViewController定义delegate方法
  6. 然后,您可以从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) }