3

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?

Diego
  • 366
  • 3
  • 18
  • check if navigation controller is not nil...? – Adnan Aftab Oct 21 '14 at 14:37
  • 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 Oct 21 '14 at 14:51

3 Answers3

1

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];
}];
Parag Shinde
  • 176
  • 11
1

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];
}
Rick
  • 1,818
  • 1
  • 15
  • 18
0

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.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Sorry, i should have specified, i'm using xib files. – Diego Oct 21 '14 at 14:34
  • Understood. I'll keep this answer here in case future readers, who are using storyboards, stumble across this question. – Rob Oct 22 '14 at 16:24