NSTreeController:“canInsert”的自定义行为绑定
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个可行的解决方法(尽管我仍然认为这应该通过使用 keyPathForValuesAffectingCanInsert 来解决)。欢迎提出建议。
Here's a workaround that does work (although I still think this should be solved by using
keyPathForValuesAffectingCanInsert
). Suggestions are welcome.