Check if UIView is displaying a UIAlertView

2019-02-14 12:45发布

Is it possible to determine if the current UIView has a UIAlertView on display (other than setting a variable every time a UIAlertView is created).

I'm thinking something along the lines of

    if ([self.view.subviews containsObject:UIAlertView]) { ... }

But that obviously doesn't work.

3条回答
可以哭但决不认输i
2楼-- · 2019-02-14 13:23

I think it will work:

-(BOOL) doesAlertViewExist 
{
    if ([[UIApplication sharedApplication].keyWindow isMemberOfClass:[UIWindow class]])
    {
        return NO;//AlertView does not exist on current window
    }
    return YES;//AlertView exist on current window
}
查看更多
戒情不戒烟
3楼-- · 2019-02-14 13:25

This will not work in iOS7 and above.

[alertView Show] adds subview on main window I guess.

for (UIWindow* window in [UIApplication sharedApplication].windows){
    for (UIView *subView in [window subviews]){
        if ([subView isKindOfClass:[UIAlertView class]]) {
            NSLog(@"has AlertView");
        }else {
            NSLog(@"No AlertView");
        }
    }
}

查看更多
4楼-- · 2019-02-14 13:35

If you store the UIAlertView as a property on the view controller that is displaying it and then run your code:

if ([self.view.subviews containsObject:self.myalertview]) { ... }

That should work.

查看更多
登录 后发表回答