多层可可绑定
当我绑定到多级键路径(例如 objectValue.person.photo
)时,它不会在人物更改时更新,仅在照片更改时更新。这似乎是一个问题,因为只有路径中的最后一个键被观察到有变化。
是否可以观察多个级别的绑定?例如,在 SproutCore 中,如果您在路径中放置一个星号,则将观察其后面的所有内容是否发生变化 (objectValue*person.photo
)。
When I bind to a multiple level keypath, say objectValue.person.photo
, it does not update when the person changes, only when the photo changes. This would seem to be a problem with only the last key in the path being observed for changes.
Is it possible to observe multiple levels of bindings? For instance, in SproutCore, if you place an asterisk in the path, everything after it will be observed for changes (objectValue*person.photo
).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的绑定在
objectValue.person
更改时没有更新,则通常意味着objectValue
中的任何对象都不符合键的键值观察
通过正确实现的对象,沿 keyPath 的非叶突变可以正常工作。例如,从基本的非文档 Cocoa 应用程序模板开始,我编写了以下示例:标头:
实现:
然后在 .xib 中,我添加了三个按钮,调用每个 IBActions 并添加了一个其 value 属性已绑定的标签到 keyPath 为 car.driver.name 的 App Delegate
按下任何按钮都会导致绑定标签更新,尽管事实上只有其中一个按钮实际上修改了绑定指向的确切值关键路径(
car.driver.name
)。 KVO 合规性通过标准 @synthesized 属性免费提供,因此无论它们来自 keyPath 的哪个级别,我们都能获得适当的更新。简而言之,绑定按照您希望的方式工作(即,它们针对复合 keyPath 中非叶节点键的更改进行更新)。
objectValue
或person
中的对象实现存在缺陷,导致无法正常工作。我会去那里看看。另请注意,如果其中一个是集合,则观察集合与观察集合中的所有对象不同。有关详细信息,请参阅此页面。
PS:是的,我知道这个例子会泄漏内存。您可以自己想象相关的
-dealloc
方法。If your bindings are not updating when
objectValue.person
is changed, then that usually means that whatever object is inobjectValue
is not Key-Value Observing compliant for the keyperson.
With properly implemented objects, non-leaf mutations along a keyPath work fine. For instance, starting from the base non-document Cocoa Application template, I cooked up the following example:Header:
Implementation:
Then in the .xib, I added three buttons, calling each of the IBActions and added a label whose value property was bound to App Delegate with a keyPath of
car.driver.name
Pushing any of the buttons will cause the bound label to update, despite the fact that only one of them actually modifies the exact value pointed to by the bindings keyPath (
car.driver.name
). KVO compliance comes for free with standard @synthesized properties, so we get proper updates no matter what level in the keyPath they come from.In short, bindings work the way you want them to (i.e. they update for changes to non-leaf-node keys in a compound keyPath). There's something in the implementation of the objects in
objectValue
orperson
that's deficient and preventing this from working. I would look there.Also note, in case one of these things is a collection, that observing a collection is not the same thing as observing all the objects in a collection. See this page for more info on that.
PS: Yes, I know the example leaks memory. You can imagine the relevant
-dealloc
methods for yourself.