views:

297

answers:

2

In CoreData, I have the data graph with some entities, and each object is populated in a view controller, at a defined screen, I want to pop out some (>1) objects to return to a define screen.

I tried to pop the view controllers out of the navigation stack with these lines of code:

ObjectA *objectA = objectD.objectC.objectA;
NSLog(@"objectA name: %@", objectA.name);
MyViewController    *controller = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil withObjectA:objectA];
[self.navigationController popToViewController:controller animated:YES];
[controller release];

The objectA, objectB, objectC, objectD are all objects from my data graph(with the inverse relationship, I can query back the objectA from the objectD through objectC)

The following error message is raised:

Assertion failure in -[UINavigationController popToViewController:transition:], /SourceCache/UIKit_Sim/UIKit-984.38/UINavigationController.m:1807

There are two questions here:

  • If the two objects are different, how comes they have the same name? the inverse relationship can not get back the objectA in which I used to initialize MyViewController?
  • How do you normally do popToViewController? How can I implement save/load the current state of my navigation controller so that when the application quits, I can reload the navigation controller? What are the best practices?
+1  A: 

This has nothing to do with Core Data. You are creating a new view controller when you should just reference your existing MyViewController instance. The newly created controller is obviously not on the navigation controller's stack and so you get an exception when you try to switch to it.

Use the self.navigationController.viewControllers array to reference the controller that's already on the nav controller's stack.

Ole Begemann
yeah, thanks Ole, I have just realized that :-s stupid me :(
sfa
I have the other question, what are the strategies to save/load the configuration of the application that uses navigation controller when the user quits the application?
sfa
You should ask that other question in a different post.
Marcus S. Zarra
hi Marcus, thanks for your suggestion. I am thinking if I should ask another question or do some more researches before posting ;)
sfa
+1  A: 

popToViewController: can only take you back to an existing ViewController that's already on the UINavigationController's stack. You're allocating a completely new controller and then trying to pop to it.

The Core Data object graph of your model objects isn't directly relevant here; all popToViewController: is dealing with is the stack of ViewController objects.

To pop to a specific ViewController, you need a reference to that ViewController object.

David Gelhar