2

Assuming I present a modal view B based on A, and then present a modal view C based on B.

Is there any way to dismiss both B and C and go back to A directly?

Dragon warrior
  • 1,594
  • 2
  • 23
  • 37

4 Answers4

5

Assuming you use storyboards in XCode4.5, you can use the new unwind segue. Implement

 -(IBAction)back:(UIStoryboardSegue*)segue

in A. (You can leave the function definition empty.) Then in the controller of C right click on the new exit icon and connect the new entry to the control that shall initiate the unwinding.

ilmiacs
  • 2,488
  • 13
  • 20
5

yes, i have some solution, probably it is a little bit dirty but you can work around

UIViewController* vc = self;

while (vc) {
    UIViewController* temp = vc.presentingViewController;
    if (!temp.presentedViewController) {
        [vc dismissViewControllerAnimated:YES completion:^{}];
        break;
    }
    vc =  temp;
}

you can call this from any of model controllers in your stack, all of them will be dismissed. We are just searching for the second one and then dismiss it.

IronManGill
  • 7,177
  • 2
  • 28
  • 51
Ezeki
  • 1,509
  • 10
  • 17
  • Thanks for save my time... :P – Mihir Oza Jun 23 '15 at 06:34
  • can you illustrate it , i can't figure out how you get the temp (the one that presented vc ) , then , only if temp doesn't have presented views dismiss it , but when will it not has presented views – Alsh compiler Jan 06 '16 at 15:38
2

Yes, you can call [B dismissModalViewControllerAnimated:YES];

this will dismiss B. of course, if C is presented from B, it will be dismissed too.

And, here is relative question, it contains complete answer. iPhone - dismiss multiple ViewControllers

Community
  • 1
  • 1
Alexander Tkachenko
  • 3,182
  • 1
  • 31
  • 46
0

if there're 2 ViewContronllers, then:

[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];

if there are 3 ViewControllers, then :

self.presentingViewController.presentingViewController.presentViewController

candidJ
  • 3,972
  • 1
  • 20
  • 28
Leo Chen
  • 11
  • 4