NSTreeController KVO 问题

发布于 2024-08-16 12:37:41 字数 940 浏览 4 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼睛会笑 2024-08-23 12:37:41

首先,“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文