1

In iOS, presenting a modalViewController is super easy:

[self presentModalViewController:controller animated:YES];

Or:

[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentModalViewController:controller animated:YES];

Within a modalViewController(such as navigationController), you can present a modalViewController again. Is there a way to detect how many modal view controllers are being presented? Otherwise maybe hooking the presentModalViewController:animated: and dismissModalViewControllerAnimated: would be a good idea? Thanks!

Anusha Kottiyal
  • 3,855
  • 3
  • 28
  • 45
Hang
  • 469
  • 1
  • 4
  • 18

2 Answers2

2

Check this question:

Is it possible to determine whether ViewController is presented as Modal?

You can use that code to "climb up" your views until you find a view that is not a modal view controller. Like:

while (isModal)
{
    currentViewController = currentViewController.presentingViewController;

    ...
}
Community
  • 1
  • 1
jimpic
  • 5,360
  • 2
  • 28
  • 37
0

I think I found a way to find the top most viewController, which should help resolving this question:

+ (UIViewController*)getTopMostViewController {
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    UIViewController *appRootViewController = window.rootViewController;
    UIViewController *topViewController = appRootViewController;
    while (topViewController.modalViewController != nil) {
        topViewController = topViewController.modalViewController;
    }
    return topViewController;

}

Hang
  • 469
  • 1
  • 4
  • 18