I recently started exploring MVVM and stuck at passing data between 2 view controllers.I have an array of items in my viewModel1 of ViewController1 and I want to pass that array to viewcontroller2. I am able to achieve it by getting the whole array from viewModel1 like below but I am not sure if this is the right way to do it.
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.
}
}
I think you have done it the right way, but alternatively you can use closures. Which are the preferred, method of communication within iOS. You will take care of memory leaks wherever necessary by using
weak
OR
unowned
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.