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. 解决虚拟机安装tomcat主机访问不到

    在wmware中安装linux后安装好数据库,JDK及tomcat后启动服务,虚拟机中可以访问,但是主机却无法访问,但是同时主机和虚拟机之间可以ping的通.解决方法是关闭虚拟机中的防火墙服务.桌面- ...

  2. xml文件生成与下载

    写在前面: 最近要做一个新的功能,点击按钮,可以根据数据生成对应的xml文件并保存.下面记录一下在做的过程的一些疑惑与问题(我就是太笨了,一些很简单的知识都不知道,不过通过这次跟蛋蛋的交流,解决了我的 ...

  3. 【状态压缩DP】【BZOJ1087】【SCOI2005】互不侵犯king

    1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3135  Solved: 1825[Submit][ ...

  4. 如何避免CSS :before、:after 中文乱码

    问题: 在进行页面开发时,经常会使用:before, :after伪元素创建一些小tips,但是在:before或:after的content属性使用中文的话,会导致某些浏览器上出现乱码. 解决方案: ...

  5. UBI介绍

    转:http://blog.csdn.net/kickxxx/article/details/6707589 目录 Table of contents Big red note User-space ...

  6. JAVA常见算法题(四)

    package com.xiaowu.demo; /** * 将一个正整数分解质因数.例如:输入90,打印出90=2*3*3*5. * * * @author WQ * */ public class ...

  7. 彻底解决DZ大附件上传问题

    个. 注意:很多人遇到修改php.ini后重应WEB服务后仍然不能生效.这种情况应该先确认一下所改的php.ini是不是当前PHP所使用的.您可以在WEB目录下建立一个php文件,内容很简单就一句话& ...

  8. 【OpenGL4.0】GLSL渲染语言入门与VBO、VAO使用:绘制一个三角形 【转】

    http://blog.csdn.net/xiajun07061225/article/details/7628146 以前都是用Cg的,现在改用GLSL,又要重新学,不过两种语言很多都是相通的. 下 ...

  9. Android服务Service具体解释(作用,生命周期,AIDL)系列文章-为什么须要服务呢?

    Android服务Service具体解释(作用,生命周期,AIDL) 近期沉迷于上班,没有时间写博客了.解衣入睡,未眠.随起床写一篇博客压压惊! ##我们android系统为什么须要服务Service ...

  10. PHP防止sql注入-JS注入

    一:为了网站数据安全,所有和数据库操作的相关参数必须做相关过滤,防止注入引起的网站中毒和数据泄漏 1.PHP自带效验函数 mysql_real_escape_string() 函数转义 SQL 语句中 ...