如何通过popViewControllerAnimated为Swift发回数据? [英] How to send data back by popViewControllerAnimated for Swift?

查看:298
本文介绍了如何通过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?

谢谢!

推荐答案

您可以使用委托传递数据


  1. 创建协议 in ChildViewController

  2. 创建委托变量 ChildViewController

  3. 在<$中扩展 ChildViewController 协议c $ c> MainViewController

  4. 提供对 MainViewController的 ChildViewController 的引用导航

  5. 定义委托方法 MainViewController

  6. 然后你可以从 ChildViewController调用委托方法

  1. Create protocol in ChildViewController
  2. Create delegate variable in ChildViewController
  3. Extend ChildViewController protocol in MainViewController
  4. Give reference to ChildViewController of MainViewController when navigate
  5. Define delegate Method in MainViewController
  6. Then you can call delegate method from ChildViewController

示例

在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆