在没有参考iOS 7的情况下找到UIAlertView

Abd*_*mer 12 cocoa-touch objective-c ios ios7

我在我的项目中使用的代码片段在这里回答:UIAlertView没有参考它

这是代码:

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

不幸的是它无法在iOS 7中运行,我无法解除警报视图.在调试时我发现在循环中它显示视图是类UITransitionView.非常混乱,因为我找不到此视图类的任何快速文档.

任何想法如何解决这个问题?

Bri*_*kel 16

在iOS7中,UIAlertView窗口不会出现-[UIApplication windows].事实上,UIAlertView本身是永远不会添加到任何窗口,-[UIAlertView window]始终nil.相反,警报视图管理放置在-[UIApplication keyWindow]其中的各种未记录的视图,而不引用警报视图.

iOS7中唯一真正的选择是实际跟踪您的警报视图.


sto*_*roj 15

iOS 7解决方案

Class UIAlertManager = objc_getClass("_UIAlertManager");
UIAlertView *topMostAlert = [UIAlertManager performSelector:@selector(topMostAlert)];
Run Code Online (Sandbox Code Playgroud)

我不确定AppStore是否可以批准,但是有效

UPD单行代码:

UIAlertView *topMostAlert = [NSClassFromString(@"_UIAlertManager") performSelector:@selector(topMostAlert)];
Run Code Online (Sandbox Code Playgroud)

  • 当然,生产中不允许这样做.对于集成测试,它可能会有所帮助,但在模拟器上它似乎不起作用.也许7.1杀了这个. (3认同)
  • 如果我使用NSClassFromString而不是objc_getClass,这样可以吗? (2认同)