NSTreeController KVO 问题
我有一个 NSTreeController,其数组绑定到 NSArrayController 子类的“items”(自定义)属性。 由于树控制器未绑定到 NSArrayController 的选择,因此我需要确保让树控制器知道在数组控制器的选择更改后必须获取项目。
我在 NSArrayController 的子类中完成了以下操作:
+ (NSSet *)keyPathsForValuesAffectingItems
{
return [NSSet setWithObjects:@"selectedObjects",nil];
}
据我所知,这应该足够了。类方法被调用但似乎没有任何效果。
如果我为 selectedObjects
实现一个观察者,它可以正常工作:
- (void)awakeFromNib;
{
[self addObserver:self forKeyPath:@"selectedObjects" options:0 context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(NSObjectController *)context;
{
if ([keyPath isEqual:@"selectedObjects"]) {
[self willChangeValueForKey:@"items"];
[self didChangeValueForKey:@"items"];
}
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
有任何线索吗?
I have a NSTreeController which array is bound to a "items" (custom) property of an NSArrayController subclass.
As the tree controller is not bound to the selection of the NSArrayController I need to make sure to let the tree controller know that items has to be fetched after the selection of the array controller changes.
I have done the following in a subclass of NSArrayController:
+ (NSSet *)keyPathsForValuesAffectingItems
{
return [NSSet setWithObjects:@"selectedObjects",nil];
}
Which should be sufficient AFAIK. The class method is called but does not seems to have any effect.
If I implement an observer for selectedObjects
it works fine:
- (void)awakeFromNib;
{
[self addObserver:self forKeyPath:@"selectedObjects" options:0 context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(NSObjectController *)context;
{
if ([keyPath isEqual:@"selectedObjects"]) {
[self willChangeValueForKey:@"items"];
[self didChangeValueForKey:@"items"];
}
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
Any clue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,“items”不是 NSArrayController 的可观察属性。您的意思是它是数组控制器管理集合的类的属性吗?即,它管理一个 Foo 数组,并且 Foo 有一个属性“items”?
无论如何,你都让事情变得比实际需要的更加困难。为什么不直接将树控制器的内容绑定到数组控制器的 Selection.items 路径呢?在极少数情况下这是不可能的。
First, "items" is not an observable property of NSArrayController. Do you mean it's a property of the class for which your array controller manages a collection? Ie, it manages an array of Foo and Foo has a property "items"?
In any case, you're making this harder than it needs to be. Why not just go ahead and bind the tree controller's contents to the array controller's selection.items path? There are few situations where this isn't possible.