iPhone: detecting if a UIAlert/UIActionSheet are open

五迷三道 提交于 2019-11-28 11:06:57
Sam Ritchie

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;
}
- (BOOL) doesAlertViewExist {
    for (UIWindow* window in [UIApplication sharedApplication].windows) {
        for (UIView* view in window.subviews) {
            BOOL alert = [view isKindOfClass:[UIAlertView class]];
            BOOL action = [view isKindOfClass:[UIActionSheet class]];
            if (alert || action)
                return YES;
        }
    }
    return NO;
}

You can also check for the view's window property:

if(actionSheet.window)
    isBeingPresented = YES;

Detecting alerts seems relatively easy, but action sheets had me stumped. In iOS 6.1 I had to go one step further

BOOL IsActionOpen(UIView* aView) {
BOOL    actionOpen = NO;
if (aView) {
    if ([aView isKindOfClass:[UIActionSheet class]]) {
        actionOpen = YES;
    }
    else if (aView.subviews.count > 0) {
        for (UIView* aSubview in aView.subviews) {
            if ( IsActionOpen( aSubview)) {
                actionOpen = YES;
                break;
            }
        }
    }
}
return actionOpen;

}

- (BOOL) isAnActionSheetOpen {
BOOL    actionOpen = NO;
for (UIWindow* w in [UIApplication sharedApplication].windows) {
    actionOpen =  IsActionOpen(w);
    if (actionOpen)
        break;
}
return actionOpen;

}

thanx for the help, but since iOS 6, the code piece doesn't work anymore. However, I fixed the issue with this code. Hope this helps

for (UIWindow* window in [UIApplication sharedApplication].windows) {
    NSArray* subviews = window.subviews;
    if ([subviews count] > 1) {
        BOOL alert = [[subviews objectAtIndex:1] isKindOfClass:[UIAlertView class]];
        BOOL action = [[subviews objectAtIndex:1] isKindOfClass:[UIActionSheet class]];

        if (alert || action)
            return YES;
    }
}
return NO;
-(BOOL)GetKeyWindow {    
        UIViewController *presentedViewController = myAppDelegate.window.rootViewController.presentedViewController;

        if (presentedViewController) {

            if ([presentedViewController isKindOfClass:[UIActivityViewController class]] || [presentedViewController isKindOfClass:[UIAlertController class]]) {        

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