Cocoa Touch 中的关键值观察
我一直在阅读“键值观察”,我发现 ViewController 会监听模型中的更改来更新视图。略有不同:模型如何知道它自己的参数已更新?
例如,我的视图上有一个按钮可以更改模型的一个参数,另一个按钮可以更改另一个参数。如果其中之一发生变化,我需要更新卷。最好的方法是:1)在 ViewController 中的每个单独的“ButtonPressed”方法中调用模型的 UpdateVolume 方法,或者让模型对其自身进行键值观察,并且每当其任何参数发生变化时,调用 UpdateVolume?如果我实现更多参数,后者似乎更方便,每个参数都由 ViewController 使用不同的方法进行更新。
谢谢, 约翰
I've been reading up on Key Value Observing and I get that the ViewController listens to changes in the model to update the view. Slightly differently: how does the model know that it's own parameters have been updated?
For example, I have a button on my view that changes one parameter of the model, and another button that changes another parameter. If either one changes, I need to update the volume. Would the best way to do this be: 1) Call the model's UpdateVolume method in each seperate "ButtonPressed" method in my ViewController OR to have the model do Key Value Observing on itself and whenever any of it parameters change, call UpdateVolume? The later seems more convenient if I implement more parameters, each updated by the ViewController w/ a different method.
thanks,
John
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让模型对自我进行观察听起来是错误的。它了解自身的一切,因此无需在其自身上使用 KVO 进行松散耦合。只需让每个按钮调用更新模型所需的任何公共模型 API 即可。该模型将实现任何逻辑并产生结果,VC 将使用 KVO 监视该结果,并相应地更新其视图。它就像一个魅力。
Having model do observation on self sounds wrong. It knows all about itself, so no need for loose coupling using KVO on itself. Just have each button call whatever public model API is needed to update the model. The model will implement whatever logic and produce a result, which will be watched by VC, using KVO, and update its views accordingly. It works like a charm.