去除掉屏幕上所有的UIAlertView

在 iOS 7 系统之前, AlertVIew 显示的原理是在屏幕上家里一层 Alertwindow , 然后AlertView 的视图加在了这个Window 上,所以可以通过获取window上的视图从而获取UIAlertView 然后令其消失,具体代码:

[objc]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. for (UIWindow* w in [UIApplication sharedApplication].windows)  
  2.     for (NSObject* o in w.subviews)  
  3.         if ([o isKindOfClass:[UIAlertView class]])  
  4.             [(UIAlertView*)o dismissWithClickedButtonIndex:[(UIAlertView*)o cancelButtonIndex] animated:YES];  
  5.     }  
  6.     }  
  7. }  

但是在iOS 7 上,这种方法就不起作用,因为显示的机制不同了,在Stack Overflow 中找到这样一篇说明:


大概的意思是,在iOS 7 中,UIAlertView 的 window 并不是显示在[UIApplication shareApplication].windows 中,而是被加入到了[UIApplication keyWindow] 中,但是并没有留出引用或者说指针,所以我们没有办法获取到;

所以在iOS 7 中,如果要使用如果要实现这个功能,就要在[alert show ] 的时候保留一份UIAlertView 的指针,在需要收到令其消失的地方,统一用保留的副本指针调用 dismissWithClickedButtonIndex ;可以通过继承UIAlertView 然后重写show 方法 或者 在 类别的扩展中重写 show ,在此就不详细说明了!


或者还有另一种方法 

[objc]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;">Class UIAlertManager = objc_getClass("_UIAlertManager");  
  2. UIAlertView *topAlertView = [UIAlertManager performSelector:@selector(topMostAlert)];  
  3. [topAlertView dismissWithClickedButtonIndex:0 animated:YES];</span>  

使用前要引用 #import <objc/runtime.h>

但是我不能保证这种方法是否能通过苹果的审核!慎用!!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值