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

标签 ios objective-c uialertview uialertcontroller

有什么方法可以判断当前是否有UIAlertView实例显示?因为可以在同一窗口级别显示多个 UIAlertView

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

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

最佳答案

方法1- 初始化警报的默认标志...如果警报未打开,则将 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
    }

方法2- 制作您自己的函数来检查是否显示任何 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.

关于ios - 如何检测 UIAlertView 当前是否正在显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28791327/

相关文章:

ios - UIBarButtonItem选择到下一个ViewController

ios - ios 11 beta 7 的 UIKeyboardWillShowNotification 问题

iphone - 目前 Siri 开放 API 可用吗?

objective-c - AppDelegate 的当前 View Controller ?

swift - 按钮不起作用

ios - swift SpriteKit : Creating a realistic driving experience 2D

ios - 本地安装的iOS应用程序有有效期限吗?

ios - 单击按钮时 UIAlertView 静默崩溃

android - iOS-Google Play 游戏服务的可用性回合制多人游戏

ios - 如何使 UIAlertView 在 swift 3 中手指抬起之前出现?