iPhone:检测 UIAlert/UIActionSheet 是否打开

发布于 2024-10-06 08:31:19 字数 227 浏览 9 评论 0原文

在我的 iOS 应用程序中,我启动了一个计时器,当它启动时,我需要能够检测是否有警报 (UIAlertView) 或操作表 (UIActionSheet) 打开。

一种方法是修改显示警报/操作表的代码 - 但不幸的是,在我的情况下这不是一个选项。

所以,问题是 - 有没有办法知道/检测警报或操作表是否已打开?

打开时是否发送任何通知,或者是否遍历视图层次结构来检测它?

谢谢

In my iOS application, I have a timer firing up, and when it fires, I need to be able to detect whether there's an Alert (UIAlertView) or an Action Sheet (UIActionSheet) open.

One way would be to modify the code presenting the alerts/actionsheets - but unfortunately this is not an option in my case.

So, the question is - is there a way of knowing/detecting whether an alert or action sheet have been opened?

Is there any notifications sent upon opening, or any traversal of the view hierarchy to detect it?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

最近可好 2024-10-13 08:31:19

他们在打开时确实会发送警报,但仅限于他们的代表 - 检查这个问题 是解决该问题的好方法。 Techzen 建议在您打开警报时将布尔标志设置为 YES,并在您关闭警报时将其设置回 NO

编辑:

既然您根本无权访问代码,为什么不尝试这段笨重的代码:

-(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;
}

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;
}
素衣风尘叹 2024-10-13 08:31:19
- (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;
}
- (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;
}
错爱 2024-10-13 08:31:19

您还可以检查视图的窗口属性:

if(actionSheet.window)
    isBeingPresented = YES;

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

if(actionSheet.window)
    isBeingPresented = YES;
拿命拼未来 2024-10-13 08:31:19

检测警报似乎相对容易,但操作表却让我难住了。在 iOS 6.1 中我必须更进一步

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;

}

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;

}

走过海棠暮 2024-10-13 08:31:19

感谢您的帮助,但从 iOS 6 开始,该代码段不再起作用。
不过,我用这段代码解决了这个问题。希望这有帮助

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;

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;
寄离 2024-10-13 08:31:19
-(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;
        } 
}
-(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;
        } 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文