使用 Cocoa Bindings 和 NSArrayController 观察模型变化
我的控制器中有一个绑定到可变数组的 NSArrayController
,它管理我的模型对象的数组。数组控制器绑定到我的 UI。效果很好。
现在,我尝试手动观察控制器类中模型中的值何时发生变化(基本上,我将更改后的模型标记为“needsToSave”以供稍后使用,但当它发生更改时,我还有一些其他任务)。
我已经阅读了 KVO,但我不完全确定我需要观察什么...... NSArrayController?对象数组?每个模型对象本身?困惑。
任何指示都会非常有帮助。提前致谢!
I've got an NSArrayController
bound to a mutable array in my controller, which manages an array of my model objects. The array controller is bound to my UI. It works well.
Now I'm trying to manually observe when a value changes in my model in my controller class (basically I'm marking the changed model as "needsToSave" for later on, but there are a few other tasks I have when it changes).
I've read up on KVO but I'm not entirely sure what I need to be observing... The NSArrayController? The array of objects? each model object itself? Confusion.
Any pointers would be very helpful. Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在模型项目添加和删除方法中,您应该开始和停止观察每个项目,以便了解发生的所有情况。这也将帮助您实现撤消。如果您需要示例代码,我知道 Hillegass 书涵盖了它(至少第二版做到了,已经检查了第三版)。您还可以查找用于实现撤消的示例代码以寻求帮助。
In your model item add and remove methods you should start and stop observing of each item in order to know about everything that happens. This will also help you implement undo. If you need sample code I know the Hillegass book covers it (at least 2nd edition did, have checked 3rd edition yet). You could also look for sample code for implementing undo for help.