找到UIAlertView而不参考它 [英] Find UIAlertView without having reference to it

查看:77
本文介绍了找到UIAlertView而不参考它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm writing application tests and want to check if an UIAlertView is currently shown. That means I don't have a pointer to it and I need to know who is the owner of the shown UIAlertView or on which views stack it can be found.

推荐答案

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

[UPDATE] Actually the solution below is not working for iOS 7, because the alert views are not bound to a specific window any more. Read here for details: https://stackoverflow.com/a/18703524/942107

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

Based on KennyTM'm advice in another question Sebastien provided link to, I've created a UIAlertView category with a method returning the UIAlertView if shown to be able to dismiss it programatically.

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

这篇关于找到UIAlertView而不参考它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆