2017-02-20 27 views
-1

我有2個ViewControllers和2個類爲他們每個人。如何在不同類之間傳遞數據?

在ViewController1中,我有一個包含用戶emailID,密碼,名字和姓氏的字典(名爲'data',類型爲[String:String])。

我想在ViewController2的類中訪問這個'data'字典並添加幾個元素(Age和Gender)。

我試圖通過創建ViewController1類的實例來訪問數據和segue。當我在控制檯上打印「數據」字典之前,在控制檯上顯示空白,如果在追加僅顯示詞典中的最新元素(即僅年齡和性別)後打印它,則打印它

在類之間傳遞字典,這樣它的元素也隨之移動,而不僅僅是一個空字典?

+0

你可以顯示你的代碼 – Ram

+0

另請參閱,[ViewControllers之間傳遞數據的Stackoverflow文檔](http://stackoverflow.com/documentation/ios/434/passing-data-between-view-controllers#t=201702200732185412699 )。 – NSNoob

+0

這可能會有所幫助:http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – Priyal

回答

0

試試這個 -

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) { 
    if (segue.identifier == "segueName") { // your segue name 
     //get a reference to the destination view controller 
     let destinationVC = segue.destination as! FirstViewController 

     //set properties on the destination view controller 
     destinationVC.dict = data // 
     //etc... 
    } 
} 

注 - 在您的目的地視圖控制器創建一個變量。

+0

是的! propertiy –

+0

我已經試過這個,但仍然進入第二個ViewController字典顯示爲一個空的字典:/ –

+1

@AkshayYerneni什麼阻礙你嘗試複製問題的答案中描述的多種方式?還是在文檔上?這實際上是一種解決方法。如果你無法使它工作,那麼你的代碼有問題,而不是在這個答案\ – NSNoob