有没有办法检测出现了多少模态视图控制器?

时间:2012-12-19 08:03:11

标签: ios cocoa presentmodalviewcontroller

在iOS中,呈现modalViewController非常简单:

[self presentModalViewController:controller animated:YES];

或者:

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

modalViewController(例如navigationController)内,您可以再次展示modalViewController。有没有办法检测出现了多少模态视图控制器?否则可能挂钩presentModalViewController:animated:dismissModalViewControllerAnimated:会是一个好主意吗?谢谢!

2 个答案:

答案 0 :(得分:2)

检查此问题:

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

您可以使用该代码“爬上”您的视图,直到找到不是模态视图控制器的视图。像:

while (isModal)
{
    currentViewController = currentViewController.presentingViewController;

    ...
}

答案 1 :(得分:0)

我想我找到了一种方法来找到最顶层的viewController,它应该有助于解决这个问题:

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

}