2

My view controller sequence is as follows:

A -> (push) -> B -> (modal) -> C

I return from C to B using an unwind segue and it works.

I would like however to return directly from C to A.

In that unwind segue which is located in B, I try to pop to root view:

-(IBAction)unwindToBfromC:(UIStoryboardSegue *)segue
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

and I get this error:

Tried to pop to a view controller that doesn't exist.

When I however execute:

[self.navigationController popToRootViewControllerAnimated:YES];

in B in a button action I return to A no problem. What's wrong?

ZviBar
  • 8,148
  • 5
  • 21
  • 30

1 Answers1

0

Since C is in the modal, there is nothing to pop to. So dismiss it instead.

[self dismissViewControllerAnimated:YES completion:nil];

jbouaziz
  • 1,483
  • 1
  • 12
  • 23
  • When I dismiss C this way I will only go back to B. – ZviBar Feb 05 '14 at 22:59
  • My bad! What if you use `[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];`? It's a bit dirty though. – jbouaziz Feb 05 '14 at 23:02
  • Then nothing happens because it is a modal, which is a root view to itself. I found a nice solution and posted a link to it in a comment to my answer. – ZviBar Feb 05 '14 at 23:14