iOS引用当前显示的UIAlertView
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的更多相关文章
- iOS系统自带的 UIAlertView 自动旋转的实现
这里主要解析 UIAlertView 的几个关键功能的实现: 随着设备屏幕的旋转而旋转: Alert弹出框,使用UIWindow来实现,就是说,不用依赖于当前显示在最前面的UIView. 实现源码参考 ...
- storyboard在ios模拟器无法显示的问题
一.问题描述 1.在原有项目新建一个名称为test的storyboard类型的文件. 2.test.storyboard添加View Controller,并设置View Controller下Vie ...
- Xamarin iOS教程之显示和编辑文本
Xamarin iOS教程之显示和编辑文本 Xamarin iOS显示和编辑文本 在一个应用程序中,文字是非常重要的.它就是这些不会说话的设备的嘴巴.通过这些文字,可以很清楚的指定这些应用程序要表达的 ...
- 关于asp.net mvc中 weiui gallery中IOS 下不显示预览图片问题的解决方式
IOS 下面不显示预览. 结果去掉了红框中的缓存部分 就可以显示了 备忘,也帮助一下需要的朋友 @*<meta http-equiv="pragma" content=&qu ...
- 给iOS项目中添加图片,并通过UIImageView引用和显示该UIImage图片
[问题] 关于iOS/iPhone中的文件选择对话框,用于用户去选择图片等文件 过程中,问题转换为,需要给当前iOS项目中,添加一个图片. 类似于Windows开发中的资源文件,其中图片文件属于资源的 ...
- IOS开发之显示微博表情
在上一篇博客中山寨了一下新浪微博,在之后的博客中会对上一篇代码进行优化和重用,上一篇的微博请求的文字中有一些表情没做处理,比如带有表情的文字是这样的“我要[大笑],[得意]”.显示的就是请求的字符串, ...
- Xamarin.ios引用第三方SDK
引言 诚然,Xamarin是个优秀的跨平台解决方案,但毕竟还是不能将Native中所有的方法都直接实现.诸如各种第三方库,也都只有java/oc原生版本的SDK,无法直接拿过来直接使用.但,不能直接拿 ...
- ionic browser+ios头部高度显示问题
ionic项目在使用ionic build browser或者打包ios时如果设置头部高度 方法如下 .bar-header { padding:; height:; } .scroll-conten ...
- Firemonkey 在 iOS 平台能显示更多的 emoji 字符
使用 Firmonkey 在显示 emoji 字符时,有些 emoji 并无法显示彩色,见下图: 经查 FMX 源码,是因为判断 emoji 的字符区段不足造成的,经过修改后,便可显示,见下图: 修改 ...
随机推荐
- ReactiveCocoa(二)
前言 通过ReactiveCocoa(一)的学习,相信大家对ReactiveCocoa有了一些基本认识吧.下面就让我们来学习ReactiveCocoa的一些基本使用吧! ReactiveCocoa基本 ...
- 1.11(java学习笔记)封装
封装将内部细节封装起来,只暴露外部接口. 比如我们的电视就将复杂的内部线路用外壳封装起来,只留下外部按钮或遥控,用户只需要知道按钮或遥控的作用就可以,无需明白电视内部是如何工作. 而且封装也保障了安全 ...
- Flash3D学习计划(二)——理解世界,取景,投影变换,并理解投影坐标系
各种坐标系都是有用的,因为某些信息只能在特定场景中才有意义. 一. 世界坐标系 世界坐标系是一个特殊的坐标系,它建立了描述其他坐标系所需要的参考框架.这就意味着,能够用世界坐标系描述其他坐标系的位置, ...
- /usr/local/lib/libz.a: could not read symbols: Bad value(64 位 Linux)
/usr/local/lib/libz.a: could not read symbols: Bad value(64 位 Linux) /usr/bin/ld: /usr/local/lib/lib ...
- 基于avalon+jquery做的bootstrap分页控件
刚开始学习avalon,项目需要就尝试写了个分页控件Pager.js:基于BootStrap样式这个大家都很熟悉 在这里推荐下国产前端神器avalon:确实好用,帮我解决了很多前端问题. 不多说了,代 ...
- Winform打砖块游戏制作step by step第二节---.画挡板
一 引子 为了让更多的编程初学者,轻松愉快地掌握面向对象的思考方法,对象继承和多态的妙用,故推出此系列随笔,还望大家多多支持. 预备知识,无GDI画图基础的童鞋请先阅读一篇文章让你彻底弄懂WinFor ...
- 程序编译时书写Makefile注意事项一例
在进行程序编译时,可能需要指定一些库的库的路径.头文件的路径,分别使用的参数选项是-L和-I,需要注意的是: 需要确保-L和-I后边的内容不为空,否则会出现意想不到的错误,而这种错误比较难以发现,引起 ...
- Redis的数据类型之String
Redis主要支持的数据类型有5种:String ,Hash ,List ,Set ,和 Sorted Set. Redis数据类型String string类型在redis中是最常见的类型,valu ...
- 15 个 Docker 技巧和提示
CLI(Command Line Interface,命令行) 好的 docker ps 输出 将 docker ps 输出通过管道重定向到 less -S,避免折行: docker ps -a | ...
- 一个java调用python的问题
使用 ProcessBuilder List<String> commands = new ArrayList(); commands.add("python"); c ...