1

Sorry but none of the similar questions provide a working solution.

New single-view project. Storyboard actions:

  • VC1 is embedded in nav-controller
  • a button in VC1 activates a push seque into VC2
  • a button in VC2 activates a modal segue into VC3
  • VC3 has a button, and a UIViewController-subclass to handle the button action

Problem statement: How can I have the button of the modal VC3 close both VC3 and VC2 at once, taking me back to VC1?

This did not work - takes me to VC2 only:

- (IBAction)dismissPressed:(id)sender {
    UINavigationController *myNavController = [self navigationController];
    [self dismissViewControllerAnimated:NO completion:^{
        [myNavController popToViewController:[myNavController.viewControllers objectAtIndex:1] animated:NO];
    }];
}

Thank you very much!

ishahak
  • 6,585
  • 5
  • 38
  • 56

3 Answers3

0

Use self.presentingViewController to find out the "previous view controller". You do not need to pass the previous view controller through the property.

- (IBAction)dismissPressed:(id)sender {
    UIViewController *presentingViewController = self.presentingViewController;
    [self dismissViewControllerAnimated:NO completion:^{
        [self.presentingViewController popViewControllerAnimated:TES];
    }];
}
maros
  • 432
  • 3
  • 17
  • Sorry @maros - I'm totally losing you. The button of this handler is part of the modal view - I don't have to check for its type. The problem statement is how to cause the button of VC3 to take me back to VC1 – ishahak Oct 21 '13 at 09:11
  • @ishahak ok, now I understand your problem. I have edited my comment. Please, check it. – maros Oct 21 '13 at 15:20
  • Hi @maros, sorry again but if you read thru the problem statement above you will see that I need to pop two VCs, while your code only dismiss the modal – ishahak Oct 21 '13 at 21:08
  • Hi @ishahak, ok. Tooke me some time, I edited my answer again :) You can access to the previous view controller by self.presentingViewController. – maros Oct 22 '13 at 07:44
0

Ok guys, here is the solution:

If you can limit yourself to ios6+, the right answer is to make the button an unwind segue as described in What are Unwind segues for and how do you use them?

But if you are like me and must comply to ios5, the solution goes likes this:

In the VC who calls the modal segue declare a delegate like this:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"segueModalVC"]) {
        ModalVC *vc = [segue destinationViewController];
        vc.prevVC = self;
    }
}

You will have to #import the header for the destination modal VC.

Now in the modal VC itself add a property to hold the previous VC like this:

@property (weak, nonatomic) UIViewController *prevVC;

And now the button's action for popping both VCs should look like this:

- (IBAction)pressedMainMenu:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
    [[self.prevVC navigationController] popViewControllerAnimated:NO];
}

[EDITED to show the final version which works perfectly and can do flip animation from VC3 back to VC1. Took me hours to finalize this but now you can enjoy it for free :) ]

IMHO the fact that the modal VC has no 'native' method to access its preceding VC is a design bug.

Community
  • 1
  • 1
ishahak
  • 6,585
  • 5
  • 38
  • 56
0

Try to Use popToRootViewControllerAnimated method to go back VC1.

- (IBAction)dismissPressed:(id)sender {
    UINavigationController *myNavController = [self navigationController];
    [self dismissViewControllerAnimated:NO completion:^{
        [myNavController popToRootViewControllerAnimated: NO];
    }];
}
Susim Samanta
  • 1,605
  • 1
  • 14
  • 30