编辑:我似乎找到了一些有用的东西。我保留了 ivar“堆栈”,现在它似乎正在工作
我一直在毫无问题地序列化几个自定义 NSObject 类。现在我想序列化我的 NavigationController 堆栈。每个 viewController 只需要保存几个属性即可重建导航树。我已经在 viewControllers 中实现了 NSCoding 协议,并成功地将它们编码为 NSData 并保存到磁盘。
当我尝试加载堆栈时,生成的数组具有正确数量的对象,但是当我尝试设置 viewController 数组时,我不断收到 EXC_BAD_ACCESS 错误。我只是以错误的方式解决这个问题吗?
//AppDelegate.m
-(void) loadDataFromDisk {
NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *programDataPath = [libraryPath stringByAppendingPathComponent:@"programData.dat"];
NSData *programData = [[NSData alloc] initWithContentsOfFile:programDataPath];
NSKeyedUnarchiver *decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:programData];
//stack is a mutable array declared in header
//stack = [decoder decodeObjectForKey:@"stack"];
stack = [[decoder decodeObjectForKey:@"stack"]retain]; //retain fixes? Seems to work
[decoder release];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after app launch
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
NSLog(@"%@",self.navigationController.viewControllers);
if ([stack count] > 1) {
self.navigationController.viewControllers = stack;
[stack release]; //retained earlier
}
return YES;
}