views:

241

answers:

2

Hi there, I have a long View Controllers hierarchy;

in the first View Controller I use this code:

SecondViewController *svc = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[self presentModalViewController:svc animated:YES];    
[svc release];

In the second View Controller I use this code:

ThirdViewController *tvc = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
[self presentModalViewController:tvc animated:YES];    
[tvc release];

and so on.

So there is a moment when I have many View Controllers and I need to come back to the first View Controller. If I come back one step at once, I use in every View Controller this code:

[self dismissModalViewControllerAnimated:YES];

If I want to go back directly from the, say, sixth View Controller to the first one, what I have to do to dismiss all the Controllers at once?

Thanks

A: 

Dismiss the top VC animated and the other ones not. If you hace three modal VC

[self dismissModalViewControllerAnimated:NO]; // First
[self dismissModalViewControllerAnimated:NO]; // Second
[self dismissModalViewControllerAnimated:YES]; // Third

EDIT: if you want to do this only with one method, save you hierarchy into an array of VC and dismiss the last object animated and the other ones not.

Espuz
If I use your code in the last VC, the second call of dismissModalViewControllerAnimated causes a crash:objc[7035]: FREED(id): message dismissModalViewControllerAnimated: sent to freed object=0x4c8e9a0Program received signal: “EXC_BAD_INSTRUCTION”.
Oscar Peli
You must do this in each VC, not all in the last one because on the second line you don't have a modal view controller over the current.The best approach can be save your VC hierarchy on an array and dismiss each one not animated but the last. You can do it on you AppDelegate
Espuz
A: 

I found the solution.

Of course you can find the solution in the most obvious place so reading from the UIViewController reference for the dismissModalViewControllerAnimated method ...

If you present several modal view controllers in succession, and thus build a stack of modal view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack. When this happens, only the top-most view is dismissed in an animated fashion; any intermediate view controllers are simply removed from the stack. The top-most view is dismissed using its modal transition style, which may differ from the styles used by other view controllers lower in the stack.

so it's enough to call the dismissModalViewControllerAnimated on the target View. I used the following code:

[[[[[self parentViewController] parentViewController] parentViewController] parentViewController] dismissModalViewControllerAnimated:YES];

to go back to my home.

Oscar Peli