Check if UIView is displaying a UIAlertView

自闭症网瘾萝莉.ら 提交于 2020-01-11 03:13:05

问题


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.


回答1:


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");
        }
    }
}




回答2:


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:


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.



来源:https://stackoverflow.com/questions/10727957/check-if-uiview-is-displaying-a-uialertview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!