在我的iOS应用程序中,我有一个计时器启动,当它触发时,我需要能够检测是否有警报(UIAlertView)或操作表(UIActionSheet)打开。
一种方法是修改显示警报/操作表的代码 - 但遗憾的是,在我的情况下,这不是一个选项。
所以,问题是 - 有没有办法知道/检测警报或行动表是否已被打开?
是否在打开时发送了任何通知,或者是否有任何遍历视图层次结构来检测它?
由于
答案 0 :(得分:17)
他们会在打开时发送提醒,但仅限于delegate
- 请检查this question以获得解决该问题的方法。 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;
}
答案 1 :(得分:12)
- (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;
}
答案 2 :(得分:2)
您还可以检查视图的窗口属性:
if(actionSheet.window)
isBeingPresented = YES;
答案 3 :(得分:2)
检测警报似乎相对容易,但行动表让我难过。在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;
}
答案 4 :(得分:1)
thanx的帮助,但自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;
答案 5 :(得分:1)
-(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;
}
}