UIAlertView在iOS里和一般的UIView不一样,有时候使用起来会有一些不便。特别要引用当前显示的UIAlertView的时候,就存在一些难度。

在iOS7以前,可以下面的代码可以解决这个问题

#pragma mark 查找当前界面有没有一个AlertView
+(BOOL)isAlert{
for (UIWindow* window in [UIApplication sharedApplication].windows) {
NSArray* subviews = window.subviews;
if ([subviews count] > 0)
if ([[subviews objectAtIndex:0] isKindOfClass:[UIAlertView class]])
return YES;
}
return NO;
}
#pragma mark 关闭当前界面上的alertView
+(void)closeAlert{
for (UIWindow* window in [UIApplication sharedApplication].windows) {
NSArray* subviews = window.subviews;
if ([subviews count] > 0)
if ([[subviews objectAtIndex:0] isKindOfClass:[UIAlertView class]])
[[subviews objectAtIndex:0] dismissWithClickedButtonIndex:0 animated:YES];
}
}

可以把它放在一个公用的类中作为静态方法调用,使用起来非常方便。

不幸的是,iOS7以后不能使用了。事实上,在iOS7以后,UIAlertView已经不属于任何一个window了,-[UIAlertView window]的值一直是nil。 而且alert view 的管理方法在开发文档里也没有列出来。这意味着,即使遍历整个windows的subviews,也找不到AlertView。

判断当前keyWindow

/// 查找当前界面有没有一个AlertView.
+(BOOL)isAlert{
if ([[UIApplication sharedApplication].keyWindow isMemberOfClass:[UIWindow class]])
{
////There is no alertview present
return NO;
}
return YES;
}

这个方法看起来是比较简单的,可惜无法引用到UIAlertView,就无法用代码关闭它。我尝试用

if ([[UIApplication sharedApplication].keyWindow isMemberOfClass:[UIWindow class]])
{
////There is no alertview present
return ;
}
UIAlertView* alert=(UIAlertView*)[UIApplication sharedApplication].keyWindow;

这样的代码,但是失败了。

国外有提出下面的iOS7处理方案:

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

我没有成功运行这个代码,最重要原因我希望能写一个公用的方法获取当前的UIAlertView,所以这个方法我不感兴趣。

上面代码也可以这样写:

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

感兴趣的同学可以试一下。但据说这个方法Apple Store是不会审核通过的,因为它用到的是未公开的方法。

在当前ViewController定义一个isAlertView变量

这个方法原理比较简单,但使用起来挺麻烦。

// initialize default flag for alert... If alert is not open set isOpenAlert as NO
BOOL isAlertOpen;
isAlertOpen = NO;
if (isAlertOpen == NO) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Alert is Open" delegate:self cancelButtonTitle:@"Okay!!" otherButtonTitles: nil];
[alert show];
// Now set isAlertOpen to YES
isAlertOpen = YES;
}
else
{
//Do something
}

使用Notification

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

然后在aWindowBecameVisible里验证:

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

这个方法也有点麻烦。

在AppDelegate定义公用变量

在AppDelegate.h里:

@property (nonatomic, assign) BOOL isAlertVisibleOnAppWindow;

在使用UIAlertView的时候:

AppDelegate *delegate = (AppDelegate *) [UIApplication sharedApplication].delegate;
if (!delegate.isAlertVisibleOnAppWindow) {
delegate.isAlertVisibleOnAppWindow = YES; UIAlertView *alertView = [[UIAlertView alloc] init…//alert init code }

在按钮的点击事件里,要给isAlertVisibleOnAppWindow再赋值。

这方法也不容易。

自定义一个MyUIAlertView

使用一个static BOOL alertIsShowing变量,然后override -(void)show selector.

在show的时候,就可以判断当前alertIsShowing的值。

而且可以自己定义一个close方法。

UIAlertController

苹果官方文档介绍,UIAlertView在iOS8以后不赞成再继续使用,同样UIAlertViewDelegate可能也要废弃了。使用UIAlertController来替代UIAlertView。关于UIAlertController的用法我在下一篇博文里介绍,这里还是尝试能否查找到现有UIAlertController。下面的代码经测试可以成功运行:

@implementation StringUtils
/// 查找当前界面有没有一个AlertView.
+(BOOL)isAlert{
if ([[UIApplication sharedApplication].keyWindow isMemberOfClass:[UIWindow class]])
{
return NO;
}
return YES;
} /// 关闭当前界面上的alertView.
+(void)closeAlert{
UIViewController* c=[self activityViewController];
if([c isKindOfClass:[UIAlertController class]]){
NSLog(@"success");
}
else if([c isKindOfClass:[UINavigationController class]]){
UINavigationController* d =(UINavigationController*)c; if([d.visibleViewController isKindOfClass:[UIAlertController class]]){
UIAlertController* control=(UIAlertController*)d;
[control dismissViewControllerAnimated:YES completion:^{}];
NSLog(@"success again");
}
}
}
/// 查找当前活动窗口.
+ (UIViewController *)activityViewController
{
UIViewController* activityViewController = nil; UIWindow *window = [[UIApplication sharedApplication] keyWindow];
if(window.windowLevel != UIWindowLevelNormal)
{
NSArray *windows = [[UIApplication sharedApplication] windows];
for(UIWindow *tmpWin in windows)
{
if(tmpWin.windowLevel == UIWindowLevelNormal)
{
window = tmpWin;
break;
}
}
} NSArray *viewsArray = [window subviews];
if([viewsArray count] > 0)
{
UIView *frontView = [viewsArray objectAtIndex:0]; id nextResponder = [frontView nextResponder]; if([nextResponder isKindOfClass:[UIViewController class]])
{
activityViewController = nextResponder;
}
else
{
activityViewController = window.rootViewController;
}
} return activityViewController;
}
@end

调用时,使用

[StringUtils closeAlert];

即可关闭当前打开的UIAlertController窗口。

iOS引用当前显示的UIAlertView的更多相关文章

  1. iOS系统自带的 UIAlertView 自动旋转的实现

    这里主要解析 UIAlertView 的几个关键功能的实现: 随着设备屏幕的旋转而旋转: Alert弹出框,使用UIWindow来实现,就是说,不用依赖于当前显示在最前面的UIView. 实现源码参考 ...

  2. storyboard在ios模拟器无法显示的问题

    一.问题描述 1.在原有项目新建一个名称为test的storyboard类型的文件. 2.test.storyboard添加View Controller,并设置View Controller下Vie ...

  3. Xamarin iOS教程之显示和编辑文本

    Xamarin iOS教程之显示和编辑文本 Xamarin iOS显示和编辑文本 在一个应用程序中,文字是非常重要的.它就是这些不会说话的设备的嘴巴.通过这些文字,可以很清楚的指定这些应用程序要表达的 ...

  4. 关于asp.net mvc中 weiui gallery中IOS 下不显示预览图片问题的解决方式

    IOS 下面不显示预览. 结果去掉了红框中的缓存部分 就可以显示了 备忘,也帮助一下需要的朋友 @*<meta http-equiv="pragma" content=&qu ...

  5. 给iOS项目中添加图片,并通过UIImageView引用和显示该UIImage图片

    [问题] 关于iOS/iPhone中的文件选择对话框,用于用户去选择图片等文件 过程中,问题转换为,需要给当前iOS项目中,添加一个图片. 类似于Windows开发中的资源文件,其中图片文件属于资源的 ...

  6. IOS开发之显示微博表情

    在上一篇博客中山寨了一下新浪微博,在之后的博客中会对上一篇代码进行优化和重用,上一篇的微博请求的文字中有一些表情没做处理,比如带有表情的文字是这样的“我要[大笑],[得意]”.显示的就是请求的字符串, ...

  7. Xamarin.ios引用第三方SDK

    引言 诚然,Xamarin是个优秀的跨平台解决方案,但毕竟还是不能将Native中所有的方法都直接实现.诸如各种第三方库,也都只有java/oc原生版本的SDK,无法直接拿过来直接使用.但,不能直接拿 ...

  8. ionic browser+ios头部高度显示问题

    ionic项目在使用ionic build browser或者打包ios时如果设置头部高度 方法如下 .bar-header { padding:; height:; } .scroll-conten ...

  9. Firemonkey 在 iOS 平台能显示更多的 emoji 字符

    使用 Firmonkey 在显示 emoji 字符时,有些 emoji 并无法显示彩色,见下图: 经查 FMX 源码,是因为判断 emoji 的字符区段不足造成的,经过修改后,便可显示,见下图: 修改 ...

随机推荐

  1. StringBuffer 清空

    几种方法: 方法1: 1 2 3 4 StringBuffer my_StringBuffer = new StringBuffer(); my_StringBuffer.append('hellow ...

  2. luogu P3818 小A和uim之大逃离 II

    题目背景 话说上回……还是参见 https://www.luogu.org/problem/show?pid=1373 吧 小a和uim再次来到雨林中探险.突然一阵南风吹来,一片乌云从南部天边急涌过来 ...

  3. POJ 2100:Graveyard Design(Two pointers)

    [题目链接] http://poj.org/problem?id=2100 [题目大意] 给出一个数,求将其拆分为几个连续的平方和的方案数 [题解] 对平方数列尺取即可. [代码] #include ...

  4. po_文件格式[转]

    原文: http://cpp.ezbty.org/content/science_doc/po_%E6%96%87%E4%BB%B6%E6%A0%BC%E5%BC%8F 摘要:PO 是一种 GNU 定 ...

  5. 把我的漫画浏览器后台程序迁移到GAE上了

    这两天看了一下Python和GAE相关资料,作为练手,把我以前写的Windows 8下看漫画的程序的后台解析算法迁移到了GAE上了. 之前由于没有后台服务器,很多东西在本地实现起来不是很方便,现在拿G ...

  6. java实现点选汉字验证码(自己修改后的)

    参考:http://blog.csdn.net/qq_26680031/article/details/51168527 package com.rd.p2p.web; import java.awt ...

  7. python3使用configparser解析配置文件

    http://www.jb51.net/article/87402.htm 需要注意的是每一个字段后面的值外面没有引号,切记,自己第一次配置时,加了引号,搞了半天 没找到错误,, 在用Python做开 ...

  8. 输入法不能使用ctrl+shift进行切换的问题

    第一种情况就是,你的输入法只有一种(而且这种输入法并不是“中文(简体) 微软拼音输入法”). 如果是只有一种输入法的话,是无法进行切换的,如果你是想要把输入法切换到无输入法状态,那么你可以通过设置任务 ...

  9. EM算法和GMM模型推导

  10. 熊猫猪新系统測试之四:Ubuntu 14.04

    眼下猫猪在办公室一般用的就是乌班图系统,一方面原因是老本本性能跑不起来Windows,更重要的是本猫认为Linux系统更开放些.况且如今用的也比較熟了,全然能够脱离Windows鸟!这一系列4篇新系统 ...