NSCoding序列化iPhone NavigationController堆栈

时间:2010-05-16 15:58:12

标签: iphone objective-c ipad nscoding

编辑:我似乎找到了一些有帮助的东西。我保留了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;

}

1 个答案:

答案 0 :(得分:0)

我必须在从磁盘加载后获得viewController堆栈。显然,如果您没有立即将数据分配给保留属性,它就会消失。