Cocoa:从磁盘取消归档数据后刷新 NSObjectController

发布于 2024-08-09 20:55:01 字数 504 浏览 1 评论 0原文

我有一个使用 NSObjectController 绑定到用户界面的对象。我能够存档该对象并稍后取消存档。到目前为止效果很好。在调试器中,我可以看到该对象保存了我在上一个会话中保存的数据。剩下的问题是:用户界面不刷新。我想我必须以某种方式告诉 NSObjectController 他必须处理另一个对象。但我不知道怎么办。我尝试了 newObject 但根本不起作用。

目前我的代码如下所示:

if ([aOpenPanel runModal] == NSOKButton)
{
    NSString *filename = [aOpenPanel filename];
    rpgCharacter = [NSKeyedUnarchiver unarchiveObjectWithFile:filename];

    // [myCharacterController DoSomething] ???
}

rpgCharacter 应该是 myCharacterController 的对象。

I have an Object bound to the user interface with a NSObjectController. I am able to archive the Object and unarchive it later. This works fine so far. In the Debugger I can see the object holds the data I saved in a previous session. The remaining problem is: The user interface does not refresh. I guess I have to tell the NSObjectController somehow he has to deal with an other object. But I don't know how. I tried newObject but that did not work at all.

At the moment my code looks like this:

if ([aOpenPanel runModal] == NSOKButton)
{
    NSString *filename = [aOpenPanel filename];
    rpgCharacter = [NSKeyedUnarchiver unarchiveObjectWithFile:filename];

    // [myCharacterController DoSomething] ???
}

rpgCharacter should be the object for the myCharacterController.

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

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

发布评论

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

评论(1

眼中杀气 2024-08-16 20:55:01

您正在做的是直接设置 rpgCharacter iVar。为了触发 KVO,您需要以不同的方式执行此操作:

如果您使用的是 Objective-C 2.0 和属性语法:

if ([aOpenPanel runModal] == NSOKButton)
{
    NSString *filename = [aOpenPanel filename];
    self.rpgCharacter = [NSKeyedUnarchiver unarchiveObjectWithFile:filename];

}

或者,如果您直接使用 KVC 并且有一个正确命名的 setter:

if ([aOpenPanel runModal] == NSOKButton)
{
    NSString *filename = [aOpenPanel filename];
    [self setRpgCharacter:[NSKeyedUnarchiver unarchiveObjectWithFile:filename]];

}

What you are doing is setting the rpgCharacter iVar directly. In order to trigger KVO you need to do this in a different way either:

if you are using Objective-C 2.0 and property syntax:

if ([aOpenPanel runModal] == NSOKButton)
{
    NSString *filename = [aOpenPanel filename];
    self.rpgCharacter = [NSKeyedUnarchiver unarchiveObjectWithFile:filename];

}

or, if you are using KVC directly and have a correctly named setter:

if ([aOpenPanel runModal] == NSOKButton)
{
    NSString *filename = [aOpenPanel filename];
    [self setRpgCharacter:[NSKeyedUnarchiver unarchiveObjectWithFile:filename]];

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