找到UIAlertView而不参考它

Gam*_*ril 2 iphone ios

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

Gam*_*ril 8

[ 更新 ]实际上,下面的解决方案不适用于iOS 7,因为警报视图不再绑定到特定窗口.有关详细信息,阅读此处:https://stackoverflow.com/a/18703524/942107

基于KennyTM的建议在Sebastien提供链接的另一个问题中,我创建了一个UIAlertView类,其中一个方法返回UIAlertView,如果显示能够以编程方式解除它.

UIAlertViewAdditions.h:

#import <UIKit/UIKit.h>

@interface UIAlertView (UIAlertViewAdditions)

+ (UIAlertView *) getUIAlertViewIfShown;

@end
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

  • 不幸的是,这并没有解决ios7的错误 (2认同)