How to get the Controller that the screen is showing in iOS

  1. //Get the ViewController currently displayed by Window  
  2. - (UIViewController*)currentViewController{  
  3.     //Get the root view of the currently active window  
  4.     UIViewController* vc = [UIApplication sharedApplication].keyWindow.rootViewController;  
  5.     while (1)  
  6.     {  
  7.         //According to different page switching methods, gradually obtain the top viewController  
  8.         if ([vc isKindOfClass:[UITabBarController class]]) {  
  9.             vc = ((UITabBarController*)vc).selectedViewController;  
  10.         }  
  11.         if ([vc isKindOfClass:[UINavigationController class]]) {  
  12.             vc = ((UINavigationController*)vc).visibleViewController;  
  13.         }  
  14.         if (vc.presentedViewController) {  
  15.             vc = vc.presentedViewController;  
  16.         }else{  
  17.             break;  
  18.         }  
  19.     }  
  20.     return vc;  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324789600&siteId=291194637