Search code examples
iphonemodalviewcontrollerstacked

Automatically dismiss underling modal view


I'm sharing this as it took me A while to figure out. This is if you need to get rid of a double stack of modal views IF it is pressent.

if(self.parentViewController.parentViewController)
        [self.parentViewController.parentViewController dismissModalViewControllerAnimated:YES];
else
    [self dismissModalViewControllerAnimated:YES];

I have a view that sometimes gets called from a modal view. In that case I would need to get rid of both views at the same time. While dealing with the situation where it was the only modal view. This worked.


Solution

  • As of xCode 4.2 this is no longer working, The new way to deal with this situation is:

    if(self.presentingViewController.presentingViewController)
        [self.presentingViewController.presentingViewController dismissModalViewControllerAnimated:YES];
    else
        [self dismissModalViewControllerAnimated:YES];
    

    As pointed out by @Hollance in a relevant thread of mine:

    iOS 5 SDK treating UIViews differently

    "There is a new property in iOS 5 named presentingViewController. The meaning of parentViewController got changed a bit with the new container view controller API, so it may not always be set when you think it is. That's what presentingViewController is now for."