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 的字符区段不足造成的,经过修改后,便可显示,见下图: 修改 ...
随机推荐
- Frequency
题目描述 Snuke loves constructing integer sequences. There are N piles of stones, numbered 1 through N. ...
- java 概括
作者:Dnvce链接:https://www.zhihu.com/question/61950442/answer/441166734来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载 ...
- [COCI2015]ZGODAN
题目大意: 给你一个数$n(n\leq10^1000)$,定义一个数是“美丽数”当且仅当这个数各个数位上的数奇偶性不同. 求最接近$n$的“美丽数”,若有多个,则依次输出. 思路: 贪心+高精度. 首 ...
- SQL表操作习题5 26~35题
- 玩转Nuget服务器搭建(一)
背景 公司项目是分模块进行架构 ...
- Delphi CRC算法, 不错
http://www.cnblogs.com/tangqs/archive/2011/12/08/2280255.html
- 设计模式之空对象模式(php实现)
github地址:https://github.com/ZQCard/design_pattern /** * 在空对象模式(Null Object Pattern)中,一个空对象取代 NULL 对象 ...
- git push后自动部署
前提,服务器已经装好ssh,本地也已经将ssh 公钥传到服务器对应位置 先用 pbcopy < ~/.ssh/PRIVATE_KEY.pub 将公钥复制到剪贴板:通过 ssh USER@SERV ...
- Jsp中如何在<c:forEach>标签内获取集合的长度
利用jstl标签functions的prefix属性的length属性值 1.首先在jsp页面导入jstl function标签 <%@ taglib prefix="fn" ...
- Git历险记(一)
[编者按]作为分布式版本控制系统的重要代表——Git已经为越来越多的人所认识,它相对于我们熟悉的CVS.SVN甚至同时分布式控制系统的 Mercurial,有哪些优势和不足呢.这次InfoQ中文站有幸 ...