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. }  
     
  • 相关阅读:
    linux___用户管理
    linux基础_vim命令
    linux
    linux_centos安装
    python基础1_哈希碰撞的认识
    oop对象,面向对象
    Oracle数据库—— 事务处理与并发控制
    Oracle数据库——索引、视图、序列和同义词的创建
    Oracle数据库——触发器的创建与应用
    Oracle数据库—— 存储过程与函数的创建
  • 原文地址:https://www.cnblogs.com/LynnAIQ/p/6076816.html
Copyright © 2011-2022 走看看