MVVM模式下向UIViewController传递数据 [英] Pass data to UIViewController under MVVM pattern

查看:43
本文介绍了MVVM模式下向UIViewController传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 MVVM 用于我的视图控制器,但我面临一个问题,我不确定什么是解决它的最佳方法.基本上,视图控制器 A 显示了一个表视图,其中填充了来自视图模型 A 的数据.每个单元格都有自己的视图模型.视图模型 A 负责创建这些视图模型并将它们暴露给视图控制器.但是现在我选择了这些项目之一,我发现我的模型(我需要传递给另一个视图控制器的数据,以便它可以根据该数据创建自己的视图模型)隐藏在视图模型后面.视图模型不应该公开模型,但是如何将这些数据传递给另一个视图控制器?单元格视图模型是否也应该公开其他视图控制器所需的所有内容并只传递该视图模型?这似乎也不对.

I'm using MVVM for my view controllers and I'm facing an issue that I'm not really sure what would be the best way to solve it. Basically, view controller A shows a table view which is populated with data from view model A. Each cell has its own view model. View model A is responsible for creating these view models and expose them to the view controller. But now that I select one of these items I found that my models (the data I need to pass to the other view controller so it can create its own view model off that data) are hidden behind the view models. View models shouldn't expose the model, but then how could I pass this data to the other view controller? Should the cell view model also expose everything that is needed by the other view controller and just pass that view model? That doesn't seem right either.

推荐答案

在评估并尝试了本文中列出的建议后,我决定采用一种方法,其中视图控制器 A 的视图模型负责创建视图视图控制器 B 的模型,考虑到它是拥有数据的模型.我从 http://www.martinrichter.net/blog/2015/08/20/navigation-with-mvvm-on-ios/ 我认为这是不破坏 MVVM 抽象的最好方法.

After evaluating and playing around with suggestions listed in this post, I decided to go with an approach where the view model of view controller A is responsible for creating the view model of view controller B, considering that it is the one having the data. I got the approach from http://www.martinrichter.net/blog/2015/08/20/navigation-with-mvvm-on-ios/ and I think is the best way to not break MVVM abstraction.

这篇关于MVVM模式下向UIViewController传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆