找到UIAlertView而不参考它

时间:2011-09-28 07:40:04

标签: iphone ios

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

1 个答案:

答案 0 :(得分:8)

[ UPDATE ]实际上,下面的解决方案不适用于iOS 7,因为警报视图不再绑定到特定窗口。请阅读此处了解详情:https://stackoverflow.com/a/18703524/942107

根据KennyTM的建议在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