绑定到 Cocoa 中的类方法?
如果我有这样的方法:
@interface CharacterSet
+ (NSArray *)allCharacterSets;
@end
我可以使用 Cocoa 绑定来绑定它吗?
我正在尝试将 NSComboBox 的内容值连接到它。当我在 IB 的“模型密钥路径”字段中输入 CharacterSet.allCharacterSets
时,它不起作用,并显示:
[ 添加观察者: forKeyPath:@"CharacterSet.allCharacterSets" 选项:0x0 上下文:0x200275b80] 是 发送到一个不是的对象 “CharacterSet”符合 KVC 标准 财产。
我不知道还能尝试什么。目前,我必须将 allCharacterSets 的返回值存储到自定义窗口控制器(或自定义窗口)中的 ivar 中才能使其正常工作,这似乎是我不必采取的额外步骤。
If I have a method like:
@interface CharacterSet
+ (NSArray *)allCharacterSets;
@end
Can I bind to it using Cocoa bindings?
I'm trying to hook up an NSComboBox's content values to it. When I enter CharacterSet.allCharacterSets
into the "Model Key Path" field in IB, it doesn't work, saying:
[
addObserver:
forKeyPath:@"CharacterSet.allCharacterSets"
options:0x0 context:0x200275b80] was
sent to an object that is not
KVC-compliant for the "CharacterSet"
property.
I'm not sure what else to try. Currently I have to store the return value of allCharacterSets
into an ivar in my custom window controller (or custom window) to make it work, which seems like an extra step I shouldn't have to take.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我正在开发一个巨大的应用程序,你提到的额外步骤对我来说真的很痛苦。所以我开发了一个小的代理类来处理类方法绑定。
首先,我添加了一个小型代理类
,然后向 NSApplication 添加了一个类别访问器
(我实际上将其添加到我的应用程序委托中并实现了 application:delegateHandlesKey:)
现在您已准备好即使在界面构建器中,也可以使用 keyPath @"classProxy.CharacterSet.allCharacterSets" 将类方法绑定到 Application 对象。
I'm working on a huge application and the extra steps you mentioned were really pain in the ass for me. So I developed a small proxy class that handles class method bindings.
First I've added a small proxy class
Then added a category accessor to NSApplication
(I actually added this to my application delegate and implemented application:delegateHandlesKey:)
Now you are ready to bind class methods to the Application object, even in the interface builder, with the keyPath
@"classProxy.CharacterSet.allCharacterSets"
.@cocoafan ..我在我热情洋溢的评论中意识到..我所说的例子可能行不通,除非你像我选择的那样实际实现你的天才答案......为了使这个令人头脑麻木的辉煌(以及简单)魔法更加通用和有用......我刚刚创建了一个基本类,正如你所描述的,但不是分类(这就是它的名字吗?)
NSApplication
,我是在NSObject
上完成的>。下面是完整的例子……
编辑… 大约 2 天后,我简单的思维折叠起来,试图记住这是谁的作品,以及为什么它如此精彩。 我会继续在这里发帖,因为我必须做一些事情来提醒自己这是什么,以及为什么我应该关心.. 这是一个基本的例子..
LOG
NSCaliberatedRGBColorSpace 1 0 0 1
@cocoafan.. I realized in my effusive comment.. that the example I stated may not work unless you were to actually implement your GENIUS answer as I had chosen to… In order to make this mind-numbingly brilliant (as well as simple) magic even more generic and useful… I just created a basic class as you had described, but instead of categorizing (is that what it's called?)
NSApplication
, i did it onNSObject
. Full example below……
Edit… After like 2 days my simple mind folded trying to remember who this works, and why it's so wonderful. I will keep posting here as I have to do things to remind myself of what this is, and why I should care.. So a basic example..
LOG
NSCalibratedRGBColorSpace 1 0 0 1