我需要通过popView将一些数据从secondView发送回First View.我如何通过popViewControllerAnimated发回数据?
谢谢!
iDh*_*val 70
您可以使用返回数据 delegate
protocol于ChildViewControllerdelegate变量ChildViewControllerChildViewController协议MainViewControllerChildViewController提供MainViewController何时参考navigatedelegate方法MainViewControllerdelegate方法ChildViewController例
在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
    }
}
有两种选择:
A)与Segue
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
   let goNext = segue.destinationViewController as ChildViewController
   goNext.delegate = self
}
B)没有塞格
let goNext = storyboard?.instantiateViewControllerWithIdentifier("childView") as ChildViewController
goNext.delegate = self
self.navigationController?.pushViewController(goNext, animated: true)
方法调用
self.delegate?.childViewControllerResponse(parameter)
小智 17
如果你想通过弹出发送数据,你可以这样做:
func goToFirstViewController() {
  let a = self.navigationController.viewControllers[0] as A
  a.data = "data"
  self.navigationController.popToRootViewControllerAnimated(true)
}