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?
iOS6 compatible version:
iOS7 compatible version:
I made a category interface that stores all instance in init method.
I know it's a very inefficient way.
Start instance monitoring before using UIAlertView.
Call dismissAll before showing another.
It's better using a singleton pattern if you can control all UIAlertViews.
But in my case, I need this code for closing javascript alert dialog in a UIWebView.
Since
UIAlertView
is deprecated in iOS8 in favor ofUIAlertController
(which is aUIViewController
, presented modally), you can't preset 2 alerts at the same time (from the same viewController at least). The second alert will simply not be presented.I wanted to partially emulate
UIAlertView
's behavior, as well as prevent showing multiple alerts at once. Bellow is my solution, which uses window'srootViewController
for presenting alerts (usually, that is appDelegate's navigation controller). I declared this in AppDelegate, but you can put it where you desire.If you encounter any sorts of problems using it, please report here in comments.