查找UIAlertView而不参考它iOS 7

我在我的项目中使用的代码片段在这里回答: 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; } 

不幸的是,它不适用于iOS 7,我无法解雇一个警报视图。 在debugging过程中,我发现在循环中显示的是UITransitionView类。 很混乱,因为我找不到这个视图类的快速文档。

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

在iOS7中, UIAlertView窗口不出现在-[UIApplication windows] 。 事实上, UIAlertView本身从来没有添加到任何窗口, -[UIAlertView window]始终nil 。 相反,警报视图pipe理放置在-[UIApplication keyWindow]的各种无文档视图,并且没有引用返回到警报视图。

你在iOS7中唯一真正的select是实际跟踪你的警报视图。

iOS 7解决scheme

 Class UIAlertManager = objc_getClass("_UIAlertManager"); UIAlertView *topMostAlert = [UIAlertManager performSelector:@selector(topMostAlert)]; 

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

UPD单行代码:

 UIAlertView *topMostAlert = [NSClassFromString(@"_UIAlertManager") performSelector:@selector(topMostAlert)]; 

我遇到了类似的问题,在我的情况下警报显示从不同的视图控制器的实例,因为布赖恩已经提到UIAlertView窗口不出现在iOS7的 – [UIApplication windows]

所以要跟踪这个以下的方法可以遵循 –

  1. 在App Delegate中定义一个BOOL常量 –

     @property (nonatomic, assign) BOOL isAlertVisibleOnAppWindow; 
  2. 在哪里'UIAlerView`存在,检查早期的实例存在 –

     AppDelegate *delegate = (AppDelegate *) [UIApplication sharedApplication].delegate; if (!delegate.isAlertVisibleOnAppWindow) { delegate.isAlertVisibleOnAppWindow = YES; UIAlertView *alertView = [[UIAlertView alloc] init…//alert init code // Either handle alert cancel/completeion click here via blocks, or use alert delegates to reset the isAlertVisibleOnAppWindow BOOL variable to NO. } 

这可能会对其他人有帮助,想到分享这个。

 UIAlertView *topMostAlert = [NSClassFromString(@"_UIAlertManager") performSelector:@selector(topMostAlert)]; 

这将不被允许发布到苹果商店。 在构buildvalidation过程中,Xcode会抛出一个错误,如:“访问未logging的方法..”所以你不能使用它,但是这个代码运行良好。

你可以注册到UIWindowDidBecomeVisibleNotification

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(aWindowBecameVisible:) name:UIWindowDidBecomeVisibleNotification object:nil]; 

并在aWindowBecameVisible中检查_UIModalItemHostingWin的窗口描述:

 if ([[theWindow description] hasPrefix:@"<_UIModalItemHostingWin"]) { // This is the alert window }