views:

29

answers:

1

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];
+1  A: 

Why would you send it back? Your root view controller has not been -dealloc'ed so it still exists in memory and still has a reference to the NSManagedObjectContext. There is nothing to pass back.

The error you are getting is not Core Data related. Run it in the debugger and set a breakpoint on objc_exception_throw and see where it is crashing.

Marcus S. Zarra
I was under the impression that view controllers were automatically dealloc'd when they were no longer the current focus of the UINavigationController. Guess that changes everything.
Rob
They are released only when they are removed from the stack. When you push another view on top it does not release the view underneath. When you pop a view controller off the stack then it is released.
Marcus S. Zarra