Search code examples
iphoneobjective-ccocoa-touchuikituiviewcontroller

How to Dismiss 2 Modal View Controllers in Succession?


I have 2 view controllers presented modally.

A presents B which presents C.

When I dismiss C I would like to dismiss B as well. But I am not sure how to do this:

Dismiss C:

[self dismissModalViewControllerAnimated:YES]
//[delegate dismissB] //this doesn't work either when i create a delegate pattern

Now I am left with B. How can I dismiss B from C?


Solution

  • Try using the next code in B (right after dismissing C, as you already do):

    [self.parentViewController dismissModalViewControllerAnimated:YES];
    

    IMPORTANT:
    Don't do anything in the method after this line.
    This view controller (B) probably will be released and deallocated...

    UPDATE:
    Starting from iOS7 the method above is deprecated.
    Use the next method instead:

    [self.parentViewController dismissViewControllerAnimated:YES completion:^{ /* do something when the animation is completed */ }];