简体   繁体   中英

Cocoa : Refresh NSObjectController after unarchiving Data from disk

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.

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]];

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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