gpt4 book ai didi

cocoa-bindings - 绑定(bind)到 Cocoa 中的类方法?

转载 作者:行者123 更新时间:2023-12-03 20:54:10 26 4
gpt4 key购买 nike

如果我有这样的方法:

@interface CharacterSet
+ (NSArray *)allCharacterSets;
@end

我可以使用 Cocoa 绑定(bind)来绑定(bind)它吗?

我正在尝试将 NSComboBox 的内容值连接到它。当我输入 CharacterSet.allCharacterSets进入IB中的“模型 key 路径”字段,它不起作用,说:

[ addObserver: forKeyPath:@"CharacterSet.allCharacterSets" options:0x0 context:0x200275b80] was sent to an object that is not KVC-compliant for the "CharacterSet" property.



我不确定还能尝试什么。目前我必须存储 allCharacterSets 的返回值进入我的自定义窗口 Controller (或自定义窗口)中的 ivar 以使其工作,这似乎是我不应该采取的额外步骤。

最佳答案

我正在开发一个巨大的应用程序,你提到的额外步骤对我来说真的很痛苦。所以我开发了一个处理类方法绑定(bind)的小型代理类。

首先,我添加了一个小型代理类

@implementation INClassProxy

- (id)valueForUndefinedKey:(NSString *)key {
return NSClassFromString(key);
}

@end

然后向 NSApplication 添加了一个类别访问器
- (id)classProxy {
static INClassProxy *proxy = nil;
if (proxy == nil)
proxy = [[INClassProxy alloc] init];
return proxy;
}

(我实际上将它添加到我的应用程序委托(delegate)中并实现了 application:delegateHandlesKey: )

现在您可以使用 keyPath @"classProxy.CharacterSet.allCharacterSets" 将类方法绑定(bind)到 Application 对象,即使在接口(interface)构建器中也是如此。 .

关于cocoa-bindings - 绑定(bind)到 Cocoa 中的类方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5722170/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com