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

相關問題