Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

小小的疑问 #16

Open
xuguoyongmac opened this issue Mar 21, 2018 · 6 comments
Open

小小的疑问 #16

xuguoyongmac opened this issue Mar 21, 2018 · 6 comments

Comments

@xuguoyongmac
Copy link

既然MVVM是解耦View和Model之间的关系的,就是独立View和Model ,这样如果要复用这个View的时候就很方便 ,比如我把这个View拖进另外一个工程,就能使用,但是你这个工程并没有独立View和model ,在View里面直接引入Model ,你觉得这个是真正意义上的MVVM?我要复用这个View 我还要有一个一样的Model,这不是耦合吗?

@Alexdara
Copy link

Model和View不是一一对应的,一个View可对应多个model,换言之将多种结构的数据在一个视图上显示出来

@xuguoyongmac
Copy link
Author

准确的说View不能知道Model的存在,更不能知道Model存在什么属性,您目前的做法肯定是在

  • (void)smk_configureViewWithViewModel:(id)viewModel {

    [viewModel smk_viewModelWithModelBlcok:^(id model) {
    ThirdModel *thirdModel = (ThirdModel *)model;
    self.testLabel.text = thirdModel.title;
    }];
    }
    在这里,已经和明显的用View 直接调用了model,如果我要绑定另外一个Model ,就只有在绑定的时候判断Model的Class然后进行强转再赋值给label。那这个View就没有重用的说法了,如如果我另一个工程需要用到这个View,我要先把这个View拖进去,然后还要拖一个ThirdModel,否则就只能修改代码,这样就无法重用了,所以我个人觉得View 是不能直接调用Model的 ,就是单纯的View,这样才方便单元测试,即使在MVC中,View也不会直接和Model交互,就像 cell.label.text = model.name 这样的语句应该在Controller中,这样才是MVC的真实架构,这也是MVC的C臃肿的其中一个原因。 至少,在这个工程里面,并没有分离出View和Model两者之间的耦合

@xuguoyongmac
Copy link
Author

当然各有各的看法,这里只是存在疑问,就是在MVVM中。类似于cell.label.text = model.name 这样的代码,应该存在什么地方。

@Alexdara
Copy link

model - viewmodel - view 理想情况下view的展示内容绑定到viewmodel,viewmodel初值即界面初值由model提供,复用view实际上需要更改与之匹配的viewmodel(构造函数),如果需要做到纯净的MVVM ,则viewmodel不能有任何复杂的数据类型,最后做出来的view都是标准控件,实际应用我目前只能做到功能内分离,即一个独立功能内部做view-model隔离

@LXManMan
Copy link

代码写的很棒,但是确实view 和model 并没有分离,还是藕断丝连的,view 也做不到复用吧

@Akui-Wildhorse
Copy link

为什么不在cell里面去mode化呢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants