zoukankan      html  css  js  c++  java
  • 获取当前控制器

    1.我们在非视图类中想要随时展示一个view时,需要将被展示的view加到当前view的子视图,或用当前view presentViewController,或pushViewContrller,这些操作都需要获取当前正在显示的ViewController。

    [objc] view plain copy
    1. //获取当前屏幕显示的viewcontroller  
    2. - (UIViewController *)getCurrentVC  
    3. {  
    4.     UIViewController *result = nil;  
    5.       
    6.     UIWindow * window = [[UIApplication sharedApplication] keyWindow];  
    7.     if (window.windowLevel != UIWindowLevelNormal)  
    8.     {  
    9.         NSArray *windows = [[UIApplication sharedApplication] windows];  
    10.         for(UIWindow * tmpWin in windows)  
    11.         {  
    12.             if (tmpWin.windowLevel == UIWindowLevelNormal)  
    13.             {  
    14.                 window = tmpWin;  
    15.                 break;  
    16.             }  
    17.         }  
    18.     }  
    19.       
    20.     UIView *frontView = [[window subviews] objectAtIndex:0];  
    21.     id nextResponder = [frontView nextResponder];  
    22.       
    23.     if ([nextResponder isKindOfClass:[UIViewController class]])  
    24.         result = nextResponder;  
    25.     else  
    26.         result = window.rootViewController;  
    27.       
    28.     return result;  
    29. }  

    2.获取当前屏幕中present出来的viewcontroller。

    [objc] view plain copy
    1. - (UIViewController *)getPresentedViewController  
    2. {  
    3.     UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController;  
    4.     UIViewController *topVC = appRootVC;  
    5.     if (topVC.presentedViewController) {  
    6.         topVC = topVC.presentedViewController;  
    7.     }  
    8.       
    9.     return topVC;  
    10. }  
     
  • 相关阅读:
    Flutter 容器(4)
    Linux 安装maven环境
    ajaxStart、ajaxStop使用注意事项
    jquery里用正则来验证密码,必须包含大小写字母,数字及特殊符号,或最少包含两种
    Nginx 相关介绍(Nginx是什么?能干嘛?配有图片示例)
    常见web攻击总结
    用php的chr和ord函数实现字符串和ASCII码互转
    消息队列设计精要
    mysql索引总结----mysql 索引类型以及创建
    MySQL事务隔离级别详解
  • 原文地址:https://www.cnblogs.com/LynnAIQ/p/6076816.html
Copyright © 2011-2022 走看看