Cocoa Touch 中的 MVC:视图和模型如何交互?
我一直认为我理解 MVC,但最近,在阅读了很多关于该主题的 Stack Overflow 帖子后,我发现 MVC 框架执行操作的方式彼此略有不同。更具体地说,在视图和模型交互的方式上,似乎有两种思想流派:
当用户与视图交互时,视图通知控制器,控制器反过来对模型做一些事情。当模型发生变化时,模型通知控制器,控制器又更新视图。
视图订阅模型。当模型发生变化时,视图似乎直接被通知它需要更新自己。
所以我的问题是:在 Cocoa Touch (iOS) 中,最好的方法是什么MVC? 我现在主要为 iOS 编写代码,并且只对该平台的最佳实践感兴趣。 (我对 ASP.NET、Rails、Backbone 等如何做事不感兴趣。)
如果有一些 KVO示例代码。谢谢。 =)
I’ve always thought I understood MVC, but lately, after reading a lot of Stack Overflow posts on the subject, I’ve discovered that the ways in which MVC frameworks do things are slightly different from one another. More specifically, in the way in which the view and model interact, there seems to be two schools of thought:
When the user interacts with the view, the view notifies the controller, and the controller in turn does something to the model. When the model changes, the model notifies the controller, which in turn updates the view.
The view subscribes to the model. When the model changes, the view seems to be notified directly that it needs to update itself.
So my question is: In Cocoa Touch (iOS), what is the best way to do MVC? I’m mainly coding for iOS these days and am interested in the best practice for this platform only. (I’m not interested in how ASP.NET, Rails, Backbone, etc. do things.)
It would be wonderful if some KVO example code could be provided. Thanks. =)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Apple 提倡使用第一种方法,我相信。
它是标准 MVC 模型(第二种方法)的修改版本,其中模型和视图完全分离。我个人认为它更干净且更具可扩展性。
Apple advocates use of the first method, I believe.
It is the modified version of the standard MVC model (the second approach), where model and view are totally separated. Personally I think that it’s cleaner and more extensible.
我发现关于这个问题最好的基础理论是由斯坦福大学一位非常有才华的老师保罗·哈加蒂教授的。我建议在 iTunes U 上查找这门课程 - 有 18 个讲座的高清视频和 ppt 文件可供学习。以下是课程网站的链接:http://www.stanford.edu /class/cs193p/cgi-bin/drupal/
我记得他非常快速但彻底地浏览了材料的 MVC 部分,使这件事变得非常清楚。另外,我不得不说,我更同意第一流派。
I found that the best basic theory on the matter is taught in Stanford University by a very talented teacher named Paul Haggarty. I recommend looking this course up on iTunes U - there are 18 lectures in HD video and ppt files to learn from. Here is a link to the course website: http://www.stanford.edu/class/cs193p/cgi-bin/drupal/
I remember that he goes through the MVC part of the material very quickly but thoroughly, making this matter very clear. Also, I would have to say that the #1 school of thought is the one I agree more with.
根据我的经验,大多数情况下应该使用(1)。
在(2)中,由于视图不拥有模型,因此您始终需要将更改从模型传递到要渲染的视图,并且如果可以从多个模型更新视图,则需要为此编写单独的代码。
在(1)中,从控制器更新视图的方法可以由多个模型使用,控制器引用模型,因此您不需要在通知中传递任何信息。
性能方面我不知道是否有任何不同,但(1)中的代码会更清晰。
In my experience, (1) should be used in most cases.
In (2), since view does not own the model, you always need to pass the changes from model to view to be rendered and if a view could be update from multiple models, you need to write separate code for that.
In (1), the method to update the view from controller could be used by multiple models, controller has reference to models thus you don't need to pass any information in the notification.
Performance wise I don't know if there's any different but the code in (1) would be a lot clearer.