iOS 获取当前展示的页面

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

 
  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. }  
  11. 如果是在AppDelegate中
  12. 如果是用UINavigationController来组织页面结构的话可以使用:

    ((UINavigationController*)appDelegate.window.rootViewController).visibleViewController;

    如果是用UITabBarController来组织页面结构的话:

    ((UITabBarController*)appDelegate.window.rootViewController).selectedViewController;
原文地址:https://www.cnblogs.com/shifu/p/5514138.html