簡體   English   中英

在 MVVM 中的兩個 ViewController 之間傳遞數據

[英]Passing Data Between two ViewControllers in MVVM

我最近開始探索 MVVM 並堅持在 2 個視圖控制器之間傳遞數據。我在 ViewController1 的 viewModel1 中有一組項目,我想將該數組傳遞給 viewcontroller2。 我可以通過從 viewModel1 獲取整個數組來實現它,如下所示,但我不確定這是否是正確的方法。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
   if segue.identifier == Constants.kShowDetailSegue {
        let destinationViewController = segue.destination as! ListViewController
        destinationViewController.products = viewModel1.getFavouriteProducts()
      //the getFavouriteProducts gets data from coredata.

    }
}

我認為您以正確的方式完成了它,但是您也可以使用閉包。 這是 iOS 內首選的通信方法。 您將在必要時使用 memory 泄漏

weak 

或者

無主

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM