2011-09-28 34 views
2

我正在编写应用程序测试,并且想要检查当前是否显示UIAlertView。这意味着我没有指向它的指针,我需要知道显示的UIAlertView的所有者是谁,或者可以在哪些视图堆栈中找到它。查找UIAlertView而不参考它

+1

可能重复:[iPhone SDK:检查是否一个UIAlertView中被示出](HTTP://计算器。 com/questions/2528929/iphone-sdk-check-if-a-uialertview-is-showing) –

+0

谢谢。检查应用程序的窗口是解决方案。没有考虑到有几个窗口。仅检查由应用程序委托人控制的人员。 – Gamadril

回答

8

[UPDATE]实际上,下面的解决方案不适用于iOS 7,因为警报视图不会再绑定到特定的窗口。在这里阅读详细信息:https://stackoverflow.com/a/18703524/942107

基于KennyTM'm另一个问题的建议Sebastien提供了链接,我创建了一个UIAlertView类别,并带有返回UIAlertView的方法,如果能够以编程方式解除它的话。

UIAlertViewAdditions.h:

#import <UIKit/UIKit.h> 

@interface UIAlertView (UIAlertViewAdditions) 

+ (UIAlertView *) getUIAlertViewIfShown; 

@end 

UIAlertViewAdditions.m:

#import "UIAlertViewAdditions.h" 

@implementation UIAlertView (UIAlertViewAdditions) 

+ (UIAlertView *) getUIAlertViewIfShown { 
    if ([[[UIApplication sharedApplication] windows] count] == 1) { 
     return nil; 
    } 

    UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; 
    if ([window.subviews count] > 0) { 
     UIView *view = [window.subviews objectAtIndex:0]; 
     if ([view isKindOfClass:[UIAlertView class]]) { 
      return (UIAlertView *) view; 
     } 
    } 
    return nil; 
} 

@end 
+1

我在我的项目中使用这个代码,现在它不在iOS7中工作:( –

+0

尝试'UIWindow * window = [[[UIApplication sharedApplication] windows] objectAtIndex:2];''和'UIView * view = [window.subviews objectAtIndex :1];'但更简洁的方法是检查类名 – Gamadril

+2

不幸的是,这并不能解决ios7的错误 – Lukas