I may just be passing data between views completely wrong here, so I am open to completely changing how I pass my data back and forth.
My app delegate creates the NSManagedObjectContext and passes that to my main menu using a UINavigationController which makes it the root view:
MainMenuViewController *mainMenuViewController = [[MainMenuViewController alloc] init];
NSManagedObjectContext *context = [self managedObjectContext];
if (!context) {
// Handle the error.
}
// Pass the managed object context to the view controller.
mainMenuViewController.managedObjectContext = context;
UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:mainMenuViewController];
self.theNavController = aNavigationController;
[[self theNavController] setNavigationBarHidden:YES animated:NO];
[window addSubview:theNavController.view];
[window makeKeyAndVisible];
[mainMenuViewController release];
[aNavigationController release];
Then when they select a different view controller from the main menu, I initialize the new view controller, pass the NSManagedObjectContext to it, then push it onto the UINavigationController:
BombsViewController *bombsViewController = [[BombsViewController alloc] init];
bombsViewController.managedObjectContext = self.managedObjectContext;
[self.navigationController pushViewController:bombsViewController animated:YES];
[bombsViewController release];
All is fine until I decide to go back to the main menu from my BombsViewController. I am attempting to use the following to pop the root view controller back onto the UINavigationController:
[self.navigationController popToRootViewControllerAnimated:YES];
However, I can't see a way to send the root view controller my NSManagedObjectContext back since the popToRootViewControllerAnimated method doesn't accept a view. I tried using the popToViewController:animated: method but then the app crashes with a "terminate called after throwing an instance of 'NSException'", "Program received signal: “SIGABRT”.":
MainMenuViewController *mainMenuViewController = [[MainMenuViewController alloc] init];
mainMenuViewController.managedObjectContext = self.managedObjectContext;
[self.navigationController popToViewController:mainMenuViewController animated:YES];
[mainMenuViewController release];