NSTreeController:“canInsert”的自定义行为绑定

发布于 2024-12-21 16:54:33 字数 768 浏览 3 评论 0原文

我有一个 Cocoa 应用程序,其中有一个由 NSTreeController 管理的 NSOutlineView

此外,还有一个用于向大纲视图添加新元素的按钮。我将按钮的 enabled 标志绑定到树控制器的 canInsert 属性。

我只想允许向大纲视图添加最多 5 个元素。之后,canInsert 应返回 NO

我创建了自己的NSTreeController子类并覆盖了canInsert,但是按钮的启用状态没有改变,因为它没有意识到树控制器已经改变添加元素时。

我还实现了:keyPathsForValuesAffectingCanInsert并尝试返回各种属性,例如content、arrangedObjects,但这里没有运气。

@implementation ILCustomTreeController

- (BOOL)canInsert
{
    return [[self arrangedObjects] count] < 5;
}

+ (NSSet *)keyPathsForValuesAffectingCanInsert
{
    return [NSSet setWithObject:@"content"]; // I also tried 'arrangedObjects'
}

@end

I have a Cocoa app with an NSOutlineView managed by an NSTreeController.

In addition there's a button for adding new elements to the outline view. I bound the button's enabled flag to the tree controller's canInsert property.

I only want to allow adding up to 5 elements to the outline view. After that, canInsert should return NO.

I created my own sub-class of NSTreeController and overwrote canInsert, but the enabled status of the button does not change, because it doesn't realize that the tree controller has changed when adding elements.

I also implemented: keyPathsForValuesAffectingCanInsert and tried returning various properties such as content, arrangedObjects, but no luck here.

@implementation ILCustomTreeController

- (BOOL)canInsert
{
    return [[self arrangedObjects] count] < 5;
}

+ (NSSet *)keyPathsForValuesAffectingCanInsert
{
    return [NSSet setWithObject:@"content"]; // I also tried 'arrangedObjects'
}

@end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

暗喜 2024-12-28 16:54:34

这是一个可行的解决方法(尽管我仍然认为这应该通过使用 keyPathForValuesAffectingCanInsert 来解决)。欢迎提出建议。

@implementation ILCustomTreeController

- (BOOL)canInsert
{    
    return [[self arrangedObjects] count] <= 4;
}

- (void)addObject:(id)object
{
    [self willChangeValueForKey:@"canInsert"];
    [super addObject:object];
    [self didChangeValueForKey:@"canInsert"];
}

- (void)insertObject:(id)object atArrangedObjectIndexPath:(NSIndexPath *)indexPath
{
    [self willChangeValueForKey:@"canInsert"];
    [super insertObject:object atArrangedObjectIndexPath:indexPath];
    [self didChangeValueForKey:@"canInsert"];
}

- (void)remove:(id)sender
{
    [self willChangeValueForKey:@"canInsert"];
    [super remove:sender];
    [self didChangeValueForKey:@"canInsert"];    
}

@end

Here's a workaround that does work (although I still think this should be solved by using keyPathForValuesAffectingCanInsert). Suggestions are welcome.

@implementation ILCustomTreeController

- (BOOL)canInsert
{    
    return [[self arrangedObjects] count] <= 4;
}

- (void)addObject:(id)object
{
    [self willChangeValueForKey:@"canInsert"];
    [super addObject:object];
    [self didChangeValueForKey:@"canInsert"];
}

- (void)insertObject:(id)object atArrangedObjectIndexPath:(NSIndexPath *)indexPath
{
    [self willChangeValueForKey:@"canInsert"];
    [super insertObject:object atArrangedObjectIndexPath:indexPath];
    [self didChangeValueForKey:@"canInsert"];
}

- (void)remove:(id)sender
{
    [self willChangeValueForKey:@"canInsert"];
    [super remove:sender];
    [self didChangeValueForKey:@"canInsert"];    
}

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