views:

115

answers:

1

EDIT: I seem to have found something that's helped. I retained the ivar "stack" and now it seems to be working

I have been serializing several custom NSObject classes without issue. Now I'd like to serialize my NavigationController stack. Each viewController only needs a couple of properties saved in order to rebuild the navigation tree. I've implemented the NSCoding protocol in the viewControllers, and successfully coded them to NSData and saved to disk.

When I attempt to load the stack, the resulting array has the correct number of objects, but I keep getting EXC_BAD_ACCESS errors when I try to set the viewController array. Am I just going about this the wrong way?

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

}

A: 

I had to -retain the viewController stack after loading it from disk. Evidently if you don't immediately assign the data to a retained property it vanishes.

JustinXXVII