zoukankan      html  css  js  c++  java
  • 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窗口。

    作者:洛洛爱吃肉 出处:http://www.cnblogs.com/dongliu/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 如果文中有什么错误,欢迎指出。以免更多的人被误导。
  • 相关阅读:
    Leetcode 238. Product of Array Except Self
    来博客园的第一天
    [LeetCode] 1020. Number of Enclaves
    [LeetCode] 921. Minimum Add to Make Parentheses Valid
    [LeetCode] 1541. Minimum Insertions to Balance a Parentheses String
    [LeetCode] 738. Monotone Increasing Digits
    [LeetCode] 1669. Merge In Between Linked Lists
    [LeetCode] 865. Smallest Subtree with all the Deepest Nodes
    [LeetCode] 376. Wiggle Subsequence
    [LeetCode] 1170. Compare Strings by Frequency of the Smallest Character
  • 原文地址:https://www.cnblogs.com/dongliu/p/6856794.html
Copyright © 2011-2022 走看看