但是在iOS 7 上,这种方法就不起作用,因为显示的机制不同了,在Stack Overflow 中找到这样一篇说明:
大概的意思是,在iOS 7 中,UIAlertView 的 window 并不是显示在[UIApplication shareApplication].windows 中,而是被加入到了[UIApplication keyWindow] 中,但是并没有留出引用或者说指针,所以我们没有办法获取到;
所以在iOS 7 中,如果要使用如果要实现这个功能,就要在[alert show ] 的时候保留一份UIAlertView 的指针,在需要收到令其消失的地方,统一用保留的副本指针调用 dismissWithClickedButtonIndex ;可以通过继承UIAlertView 然后重写show 方法 或者 在 类别的扩展中重写 show ,在此就不详细说明了!
或者还有另一种方法
使用前要引用 #import <objc/runtime.h>
但是我不能保证这种方法是否能通过苹果的审核!慎用!!
方法一:
重写show 存储show的alert引用,在适当地比方调- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated;
@interface UIAlertView (Helper)
-(void)myshow;
@end
@implementation UIAlertView (Helper)
-(void)myshow{
[self show];
[ChatService sharedChatService].helperAlert=self;//存储show的alert引用
}
@end
方法二:
在- (void)willPresentAlertView:(UIAlertView *)alertView; 中获取alert调- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated;