3
votes

I have a navigation controller A on which i push the view controller B. From B i present modally the view controller C. I need to dismiss C and pop B at the same time. I would like to that in sequence, keeping the dismiss animation first and then the pop animation from B to A. I tried without success this code:

[self dismissViewControllerAnimated:YES completion:^{
       [self.presentingViewController.navigationController popViewControllerAnimated:YES];
}];

Any suggestion on how can i achieve this?

3
check if navigation controller is not nil...?Adnan Aftab
This log in my button gives "null" NSLog(@"%@", self.presentingViewController.navigationController); Can't understand why. I need maybe to create a reference to the view controller?Diego

3 Answers

1
votes

If you are writing in C viewcontoller then :

UIViewController *pvc = self.presentingViewController;
UINavigationController *navController = [pvc isKindOfClass:[UINavigationController class]] ? (UINavigationController *)pvc : pvc.navigationController;
[self dismissViewControllerAnimated:YES completion:^{
  [navController popViewControllerAnimated:YES];
}];

or if in B view controller

[self.presentedViewController dismissViewControllerAnimated:YES completion:^{
   [self.navigationController popViewControllerAnimated:YES];
}];
1
votes

I had tried popping two times in succession before but not dismissing one and popping another one. You can try what I did and see if it works for you.

In Subview B:

- (void)subViewCController:(SubViewCController *)controller didSelectID:(NSNumber *)theID
{
    // do something with theID...
    // for my case, I popped
    // [self.navigationController popViewControllerAnimated:YES];

    // for your case
    [self dismissViewControllerAnimated:YES completion:nil];

    // Somehow, adding a small delay works for me and the animation is just nice
    [self performSelector:@selector(backToSubViewA) withObject:nil afterDelay:0.6];
}

- (void)backToSubViewA
{
    [self.navigationController popViewControllerAnimated:YES];
}
0
votes

Are you using storyboards and segues? If so, you can use unwind segue:

In your first view controller (the one you want to jump back to, not the one you're returning from), create an unwind segue action:

- (IBAction)gotoMainMenu:(UIStoryboardSegue *)segue
{
    // if you need to do anything when you return to the main menu, do it here
}

Then in storyboard, you can create a segue from the "dismiss" button to the exit outlet icon (enter image description here) in the bar above the scene, and you'll see main menu listed there.