【发布时间】:2010-05-16 15:58:12
【问题描述】:
编辑:我似乎找到了一些有用的东西。我保留了 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;
}
【问题讨论】:
标签: iphone objective-c ipad nscoding