如何检测 UIAlertView 当前是否正在显示

How to detect whether UIAlertView is currently showing

有什么方法可以判断当前是否有UIAlertView实例显示?因为可以在同一个window关卡显示多个UIAlertView

if ([self isAlertViewShowing]) {
    // do not show UIAlertView again.
} else {
    // show your UIAlertView.
}

希望有这样一种方法叫做isAlertViewShowing或者别的什么。

方法一- 初始化警报的默认标志...如果警报未打开,则将 isAlertViewShowing 设置为 NO

    Bool isAlertViewShowing;
    isAlertViewShowing = NO;
    if (isAlertViewShowing == NO){
         UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
         [alert show];
         // Now set isAlertViewShowing to YES
         isAlertViewShowing = YES;
    }
    else
    {
         //Do something
    }

方法二- 制作您自己的函数来检查是否显示 UIAlertView

- (BOOL)isAlertViewShowing{
for (UIWindow* window in [UIApplication sharedApplication].windows) {
    NSArray* subviews = window.subviews;
    if ([subviews count] > 0){
        for (id view in subviews) {
            if ([view isKindOfClass:[UIAlertView class]]) {
                return YES;
            }
        }
    }
}
return NO;
}

I recommended to use second method if number of UIAlertView instance may be more than one.