简体   繁体   English

如何使此代码符合KVO?

[英]How do I make this code KVO compliant?

I'm trying to get a grip on bindings and have set up a CPWindow with a CPTextView in xcode and bound the window and the textview to their respective outlets. 我试图掌握绑定,并在xcode中使用CPTextView设置了CPWindow,并将窗口和textview绑定到各自的出口。

I'm getting a bit stuck however trying to make the textview display the _xmlContent . 我有点卡住,但是试图使textview显示_xmlContent I've set the binding in xcode to "delegate" and the key path to xmlContent (also tried self.xmlContent and _xmlContent and all variations thereof that I could think of). 我已经将xcode中的绑定设置为“ delegate”,并将其设置为xmlContent的关键路径(还尝试了self.xmlContent和_xmlContent以及我能想到的所有变体)。

    @import <Foundation/Foundation.j>
    @import <AppKit/AppKit.j>

    @implementation AppController : CPObject
    {
        @outlet CPWindow    theWindow;
        @outlet CPTextView  codeView;

        CPString _xmlContent @accessors(property=xmlContent);
    }

    // ...

    @end

The message I'm getting now is: 我现在收到的消息是:

CPUndefinedKeyException: <AppController 0x00a177> is not key value coding-compliant for the key xmlContent

How do I make my AppController key value coding compliant for the key "xmlContent"? 如何使我的AppController键值编码符合键“ xmlContent”?

Your code looks correct. 您的代码看起来正确。 I recreated it myself and it works fine for me: 我自己重新创建了它,对我来说效果很好:

@implementation AppController : CPObject
{
    @outlet CPWindow    theWindow;

    CPString _xmlContent @accessors(property=xmlContent);
}

- (void)applicationDidFinishLaunching:(CPNotification)aNotification
{
}

- (void)awakeFromCib
{
    [theWindow setFullPlatformWindow:YES];
    [self setXmlContent:@"kitten"];

Xcode设置显示委托绑定到xmlContent

Safari显示绑定工作

You should verify that you are running the code you think you are running. 您应该验证自己正在运行您认为正在运行的代码。 One common pitfall here is that some web-browsers really like to hang on to cached copies of your code so make sure you have the "Disable Caches" option enabled in whatever browser you are testing with. 这里的一个常见陷阱是,某些网络浏览器真的很想保留代码的缓存副本,因此请确保在要测试的任何浏览器中都启用了“禁用缓存”选项。

If all else fails, change the port number of your development HTTP server and load the page from the new URL. 如果所有其他方法均失败,请更改开发HTTP服务器的端口号,然后从新URL加载页面。 This will force the browser to get fresh copies of everything (because the URL changed). 这将迫使浏览器获取所有内容的新副本(因为URL更改了)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM