Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS dismiss UIAlertView beforing showing another

I have a Utils class which shows UIAlertView when certain notifications are triggered. Is there a way to dismiss any open UIAlertViews before showing a new one?

Currenty I am doing this when the app enters the background using

[self checkViews:application.windows];

on applicationDidEnterBackground

- (void)checkViews:(NSArray *)subviews {
    Class AVClass = [UIAlertView class];
    Class ASClass = [UIActionSheet class];
    for (UIView * subview in subviews){
        if ([subview isKindOfClass:AVClass]){
            [(UIAlertView *)subview dismissWithClickedButtonIndex:[(UIAlertView *)subview cancelButtonIndex] animated:NO];
        } else if ([subview isKindOfClass:ASClass]){
            [(UIActionSheet *)subview dismissWithClickedButtonIndex:[(UIActionSheet *)subview cancelButtonIndex] animated:NO];
        } else {
            [self checkViews:subview.subviews];
        }
    }
}

This makes it easy on applicationDidEnterBackground as I can use application.windows

Can I use the AppDelegate or anything similar to get all the views, loop through them and dismiss any UIAlertViews?

like image 662
booboo-a-choo Avatar asked Mar 22 '11 23:03

booboo-a-choo


2 Answers

for (UIWindow* window in [UIApplication sharedApplication].windows) {
  NSArray* subviews = window.subviews;
  if ([subviews count] > 0)
    if ([[subviews objectAtIndex:0] isKindOfClass:[UIAlertView class]])
      [(UIAlertView *)[subviews objectAtIndex:0] dismissWithClickedButtonIndex:[(UIAlertView *)[subviews objectAtIndex:0] cancelButtonIndex] animated:NO];
}
like image 73
Sergnsk Avatar answered Nov 08 '22 01:11

Sergnsk


iOS6 compatible version:

for (UIWindow* w in UIApplication.sharedApplication.windows)
    for (NSObject* o in w.subviews)
        if ([o isKindOfClass:UIAlertView.class])
            [(UIAlertView*)o dismissWithClickedButtonIndex:[(UIAlertView*)o cancelButtonIndex] animated:YES];
like image 27
erkanyildiz Avatar answered Nov 08 '22 01:11

erkanyildiz