tags:

views:

62

answers:

2

I have a navigation based app. The root view is a list of items. From this root view you can tap on a table cell to an item's detail view. Or you can go to a form view to create a new item via the 'Add' button in the nav bar.

My question is how I can jump from the form view to the detail view once the new object has been created?

I don't want to push the detail view on top of the form view, because I want the root table view to be what the user see's after pushing the 'back' nav button form the detail view.

I've tried the following. It pops to the root view fine, but doesn't push the detail view after that..

[context save:&error];

[self.navigationController popToRootViewControllerAnimated:NO];

// display detail view
GoalDetailViewController *detailViewController = [[GoalDetailViewController alloc] initWithNibName:@"GoalDetailViewController" bundle:nil];

// Pass the selected object to the new view controller.
detailViewController.goal = goal;

[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];

Any help and direction would be much appreciated :)

Cheers!

+1  A: 

Generally you would implement the add button using a view controller displayed modally.

[self presentModalViewController:modalViewController animated:YES];

meaning it appears from the bottom of the screen (see adding a contact). Then when they press done in the top right you can push the detail view controller on the navigation controller without animating it, making the back button go back to the original list view.

DHamrick
Ah yes, that makes a lot more sense. I think I better go pay more attention to human interface guidelines. Thanks for your help!
Fabian
A: 

This isn't something you see too often in apps, but it can be accomplished like this:

// Get the current view controller stack.
NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];

// Instantiate your new detail view controller
GoalDetailViewController *detailViewController = [[GoalDetailViewController alloc] initWithNibName:@"GoalDetailViewController" bundle:nil];
detailViewController.goal = goal;

// Remove the topmost view controller from the stack
[viewControllers removeLastObject];
// Replace it with the new detail view controller
[viewControllers addObject:detailViewController];

// Change the view controller stack
[self.navigationController setViewControllers:viewControllers animated:YES];

// Clean up
[detailViewController release];

Exactly what animation you get is described here.

Robot K
Thanks for your feedback :) But it looks like the way I was trying to do it was wrong. The modal view is what I should've used. Cheers tho!
Fabian