4

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.

Smikey
  • 8,106
  • 3
  • 46
  • 74
  • 3
    Possible duplicate http://stackoverflow.com/questions/2528929/iphone-sdk-check-if-a-uialertview-is-showing – Luke May 23 '12 at 21:18
  • 1
    Similar, but the accepted answer is no good for me since I have so many instantiations, and the alternative answer is to use an undocumented method... I guess there is no quick and easy way – Smikey May 23 '12 at 21:30
  • Do you just want to know that an alert is shown or do you care where it originated from? – Paul.s May 23 '12 at 21:33
  • Just want to know if one is currently visible. Or at least that one has been displayed, as I can set an iVar to TRUE once it has been dismissed. I just don't want to set the iVar in every instance of showing an alert. – Smikey May 23 '12 at 22:15
  • can I ask what interaction scenario you are trying to create? – nielsbot May 23 '12 at 22:40

3 Answers3

16

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

Warif Akhand Rishi
  • 23,920
  • 8
  • 80
  • 107
  • This is closest to what I'm trying to achieve, so I'll give it the answer, although for some reason it didn't really work, as I'm not sure the UIAlertView remains in the subviews after it's been displayed, even though it remains on the screen. – Smikey May 24 '12 at 07:40
  • I tried this one and for some reason it doesn't work. But I found another answer at http://stackoverflow.com/a/2529692/1091926 and it works for me. Codes look similar though. (I guess its because of "if ([subviews count] > 0)") – Wayne Liu Nov 26 '12 at 07:57
  • Actually I believe it works because of the [subviews objectAtIndex:0] – Matt Hudson Jun 25 '13 at 18:47
10

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
}
Manab Kumar Mal
  • 20,788
  • 5
  • 31
  • 43
Kyle C
  • 4,077
  • 2
  • 31
  • 34
  • Although this will technically work, it opens up the potential to introduce a bug if a non UIAlertView window is displayed. – Shoerob Aug 21 '15 at 16:56
1

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.

Matt Hudson
  • 7,329
  • 5
  • 49
  • 66
  • Thanks - I was trying to avoid having to customise every alert view that I display since there are a lot of them. – Smikey May 24 '12 at 07:41