开发者

KVO and MVC Question

开发者 https://www.devze.com 2023-03-17 09:19 出处:网络
Simple question here... Is it alright in terms of MVC design to observe a value in the model from a view (using Key-Value observing), and update the view\'s current location on the screen based on whe

Simple question here... Is it alright in terms of MVC design to observe a value in the model from a view (using Key-Value observing), and update the view's current location on the screen based on when the model's variable changes?

Is it okay to observe from the view object and have that object move itself when the Location variable inside of the model object changes?

Or is this against MVC because the view and the mode开发者_JAVA百科l are communicating in a sense?


You should tie it through a C, controller, item. Even if it means you're pulling in the state data from the model, and then having the controller set the view or the view read that data from the Controller.

The view and model should always be separated by a controller. That's MVC according to Apple. The reason is that even right now it may be straightforward for you to have the view reference the model's state - but the model could change in the future, and then you'd be stuck updating the view, when there's really no reason the view should be impacted. And the model should never update the view's position - it should really not have any idea of any details of display. That's the job of a controller, to control your views and move them around based on model data.

Think about it this way: the view should only know how to display things or interact with user I/O, a model should only know about the business logic with data that comes in over an interface of inputs and outputs. You should be able to run a model without a view even existing, instead you should be able to just have unit test type code that feeds those inputs and outputs. So something like moving a view around is completely out of the responsibilities of a model.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号