0

我问question before,我找到了解决方案。现在,我需要扩大我的问题。 使用委托,如何创建委托给ViewController发送数据到ContainerView和ContainerView发送数据到ViewController在Swift中容器视图和ViewController之间的代表

+0

因此,总之你想在两个类之间传递数据? –

+0

是的,但我最大的问题是在ViewController中使用容器视图。 – James

+0

所以,对不起,在这里有点慢,你正在谈论的视图控制器嵌入在容器视图中,或者是视图控制器中的容器视图? –

回答

1

嗯,我不知道这是否完全是你要找的东西,但是我在这种情况下总是做的是在其他类中的每个视图控制器的记录。

例如,如果你的容器视图都有标识"Embed Segue"嵌入SEGUE那么你的类可能是这样的:

的SuperView类

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 
    } 
} 

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

+0

这是我已经拥有的一段代码。我必须有这段代码,而且更多的是相反的代码。 ViewControllerTwo将数据传递给ViewControllerOne。 – James

+0

@詹姆斯我编辑了我的答案,使其更清楚发生了什么事情,将数据备份到链上 –

+0

这是工作。谢谢! – James

相关问题