vicki753's iOS 基础 - 获取当前控制器

获取当前控制器

 + (UIViewController *)getCurrentVC {
  UIViewController* vc;
  UITabBarController* tbc = (UITabBarController* )[[[UIApplication sharedApplication].delegate window]     rootViewController];
    if ([tbc isKindOfClass:[UITabBarController class]]) {
          if (!tbc.presentedViewController) {
              UINavigationController* nav = tbc.selectedViewController;
              vc = [nav.viewControllers lastObject];
        } else {
              if ([tbc.presentedViewController isKindOfClass:[UINavigationController class]]) {
                    vc = [((UINavigationController* )tbc.presentedViewController).viewControllers lastObject];
            } else {
                vc = tbc.presentedViewController;
            }
        }
    } else {
            if ([tbc isKindOfClass:[UINavigationController class]]) {
                  vc = [((UINavigationController* )tbc).viewControllers lastObject];
            } else {
              vc = tbc;
          }
      }
    return vc;
}

你可能感兴趣的:(vicki753's iOS 基础 - 获取当前控制器)