views:

285

answers:

1

I'm writing a standard table view application with a number of views in the hierarchy. When I've clicked in 3-4 views, is there a way to get back to the top view? I tried loading it, but then I lose the hierarchy.

I know this command will bring me back 1 view, which is what the 'back' button does:

[self.navigationController popViewControllerAnimated:YES];
+5  A: 

You can use popToRootViewControllerAnimated: or popToViewController:animated: methods.

To get the viewcontroller to which you need to jump, get a list of all viewcontrollers from the navcontroller in an array and then select the viewcontroller from this array.

i.e. if your hierarchy is svc->svc2->vc1->vc2->vc3->vc4 and you want to go back to vc1 from vc4, do this

NSArray *viewControllers = [[self navigationController] viewControllers];
UIViewController *controller = [viewControllers objectAtIndex:2];
[[self navigationController] popToViewController:controller animated:YES];
lostInTransit
Thank you!! This exact code worked great.
cmos
This was exactly what I was looking for in my app. I pop 2 view controllers and need to set a value in the parent. I just created a value in the parent, set the value using a method call, then did the pop. Thanks!
Steven Wright