Cocoa : Refresh NSObjectController after unarchiving Data from disk
I have a开发者_C百科n 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]];
}
精彩评论