Check if a UIAlertView is showing
On the object that calls set an ivar before invoking the show method on your UIAlertView.
...
if (!self.alertShowing) {
theAlert = [[UIAlertView alloc] initWithTitle:title message:details delegate:self cancelButtonTitle:nil otherButtonTitles:@"Okay", nil];
self.alertShowing = YES;
[theAlert show];
}
...
Then in your delegate method for the alert manage setting your flag ivar to no:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
...
self.alertShowing = NO;
}
If you want the alerts to show sequentially, I would post notifications to add each message to a queue and then only take a message off the queue after an alert is dismissed.
How to detect whether UIAlertView is currently showing
Method 1-
Initialize default flag for alert... If alert is not open set isAlertViewShowing as NO
Bool isAlertViewShowing;
isAlertViewShowing = NO;
if (isAlertViewShowing == NO){
UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
// Now set isAlertViewShowing to YES
isAlertViewShowing = YES;
}
else
{
//Do something
}
Method 2-
Make your own function to check whether any UIAlertView
is showing or not
- (BOOL)isAlertViewShowing{
for (UIWindow* window in [UIApplication sharedApplication].windows) {
NSArray* subviews = window.subviews;
if ([subviews count] > 0){
for (id view in subviews) {
if ([view isKindOfClass:[UIAlertView class]]) {
return YES;
}
}
}
}
return NO;
}
I recommended to use second method if number of
UIAlertView
instance
may be more than one.
Can i check if any UIAlertView displaying right now?
For devices less than iOS 7 :-
When you make the body of the UIAlertView
, the [alertView Show]
adds a subview on the main window. So to detect the UIAlertView
you simply have to check for the subviews of the current UIView
as UIAlertView
inherits from UIView
.
for (UIWindow* window in [UIApplication sharedApplication].windows){
for (UIView *subView in [window subviews]){
if ([subView isKindOfClass:[UIAlertView class]]) {
NSLog(@"AlertView is Present");
}else {
NSLog(@"AlertView Not Available");
}
}
}
EDIT:- For devices using iOS 7
Class UIAlertManager = objc_getClass("_UIAlertManager");
UIAlertView *Alert = [UIAlertManager performSelector:@selector(Alert)];
and
UIAlertView *Alert = [NSClassFromString(@"_UIAlertManager") performSelector:@selector(Alert)];
NOTE :- If you cannot use third party API's then your only real option in iOS7 is to actually keep track of your alert views.
Check if UIView is displaying a UIAlertView
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");
}
}
}
Check if a UIAlertView is visible
You could retain a reference to it or you could set a flag in your alert delegate. When you open the alert set the flag to true and when the alert calls its dismissal delegate function flip it to false. Check the state of the flag to determine whether an alert is currently open.
If you think it even possible you could end up with a stack of alerts you might want to rethink your design. Your users will not like being hit with alert dialog after alert dialog.
What is the best way to check if a UIAlertController is already presenting?
It is not the UIAlertController that is "already presenting", it is MessagesMasterVC. A view controller can only present one other view controller at a time. Hence the error message.
In other words, if you have told a view controller to presentViewController:...
, you cannot do that again until the presented view controller has been dismissed.
You can ask the MessagesMasterVC whether it is already presenting a view controller by examining its presentedViewController
. If not nil
, do not tell it to presentViewController:...
- it is already presenting a view controller.
Check instance of UIAlertView when going in background
Remove alert in applicationDidEnterBackground
Add this line in your class
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(enteredBackground:)
name:UIApplicationDidEnterBackgroundNotification
object: nil];
And implement method as well
- (void)enteredBackground:(UIApplication *)application
{
if (mainAlertView && mainAlertView.isVisible)
[mainAlertView dismissWithClickedButtonIndex:0 animated:NO];
}
iPhone: detecting if a UIAlert/UIActionSheet are open
They do send an alert when they open, but only to their delegate
-- Check this question for a nice approach to that problem. Techzen recommends setting a boolean flag to YES
when you open up the alert, and setting it back to NO
when you dismiss the alert.
EDIT:
Since you don't have access at all to the code, why not try this clunky piece of code:
-(BOOL) doesAlertViewExist {
for (UIWindow* window in [UIApplication sharedApplication].windows) {
NSArray* subviews = window.subviews;
if ([subviews count] > 0) {
BOOL alert = [[subviews objectAtIndex:0] isKindOfClass:[UIAlertView class]];
BOOL action = [[subviews objectAtIndex:0] isKindOfClass:[UIActionSheet class]];
if (alert || action)
return YES;
}
}
return NO;
}
Related Topics
Nsurlconnection Sendasynchronousrequest Can't Get Variable Out of Closure
Xcode Is Looking for Core Data Entity Names with Dot; Not Compiling
Simulate Force Touch/3D Touch on iPhone 6S or iPhone 6S Plus Simulators
How to Release a Cgimageref in iOS
How to Set Cg_Context_Show_Backtrace Environmental Variable
Dynamic Cell Width of Uicollectionview Depending on Label Width
How to Detect Network Signal Strength in iOS Reachability
Using Multiple Storyboards in iOS
iOS 7 - Difference Between Viewdidload and Viewdidappear
Change Tab Bar Item Selected Color in a Storyboard
Iphone: Hide Uitableview Search Bar by Default
iOS App Error - Can't Add Self as Subview
Class Amsupporturlconnectiondelegate Is Implemented in Both
Counting the Number of Lines in a Uitextview, Lines Wrapped by Frame Size
Inconsistent Unicode Emoji Glyphs/Symbols
Core Data Background Context Best Practice
How to Scroll to the Bottom of a Uitableview on the iPhone Before the View Appears