【问题标题】:Delegate between Container View and ViewController in SwiftSwift 中容器视图和 ViewController 之间的委托
【发布时间】:2016-03-22 04:41:43
【问题描述】:

我问了question before,我得到了他所寻求的解决方案。现在,我需要放大我的问题。 使用委托,如何创建委托给 ViewController 向 ContainerView 发送数据,而 ContainerView 向 ViewController 发送数据

【问题讨论】:

  • 那么简而言之你想在两个类之间传递数据?
  • 是的,但我最大的问题是在 ViewController 中使用容器视图。
  • 所以,抱歉这里有点慢,你说的视图控制器是嵌入在容器视图中,还是容器视图在视图控制器中?
  • 此链接解释故事板:stackoverflow.com/questions/34298760/…
  • 对,所以你已经将一个视图控制器嵌入到另一个视图控制器中,并且你想在它们之间传递数据

标签: swift uiviewcontroller swift2 uicontainerview swift-protocols


【解决方案1】:

好吧,我不知道这是否完全是你要找的,但在这种情况下我一直在做的是记录另一个类中的每个视图控制器。

例如,如果您的容器视图具有标识符为 "Embed Segue" 的嵌入 segue,那么您的类可能如下所示:

超级视图类

Class ViewControllerOne: UIViewController {
var data = "This is my data I may want to change"
var subView: ViewControllerTwo?

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "Embed Segue" {
            let destinationVC = segue.destinationViewController as! ViewControllerTwo
            destinationVC.superView = self
            self.subView = destinationVC
        }
    }
}

嵌入式类

Class ViewControllerTwo: UIViewController {
    var data = "This is the other view controller's copy of that data"
    var superView: ViewControllerOne?
}

然后您可以通过分别引用self.subView.dataself.superView.data 在这些视图控制器之间传递数据。

编辑:为了让 ViewControllerTwo 将数据传回 ViewControllerOne,它只需引用 self.superView.data。例如:

Class ViewControllerTwo: UIViewController {
    var data = "This is the other view controller's copy of that data"
    var superView: ViewControllerOne?

    func passDataBack() {
        self.superView.data = self.data
    }
}

这将更新第一个视图控制器中的数据变量。

【讨论】:

  • 这是我已经拥有的一段代码。我必须拥有这段代码,反之亦然。 ViewControllerTwo 将数据传递给 ViewControllerOne。
  • @James 我已经编辑了我的答案,以便更清楚地将数据传回链上发生了什么
猜你喜欢
  • 1970-01-01
  • 2016-03-21
  • 1970-01-01
  • 2015-01-29
  • 1970-01-01
  • 1970-01-01
  • 2015-09-13
  • 1970-01-01
  • 2018-10-29
相关资源
最近更新 更多