美文网首页
获取当前view的Controller

获取当前view的Controller

作者: 鬼才冯三郎 | 来源:发表于2021-08-06 15:58 被阅读0次
  • (UIViewController *)getCurrentVC{

    UIViewController *currVC = nil;
    UIViewController *rootvc = self.window.rootViewController;
    do {
    if ([rootvc isKindOfClass:[UINavigationController class]]) {//控制器为UINavigationController
    UINavigationController *nav = (UINavigationController *)rootvc;
    UIViewController *vc = [nav.viewControllers lastObject];
    currVC = vc;
    rootvc = vc.presentedViewController;

      }else if ([rootvc isKindOfClass:[UITabBarController class]]){//控制器为UITabBarController
          UITabBarController *tabVC = (UITabBarController *)rootvc;
          currVC = tabVC ;
          rootvc = [tabVC.viewControllers objectAtIndex:tabVC.selectedIndex];
          continue;
      }else {//控制器为UIViewController
          UIViewController *VC = (UIViewController *)rootvc;
          currVC = VC;
          rootvc = VC.presentedViewController;
      }
    

    } while (rootvc != nil);
    return currVC;
    }

相关文章

网友评论

      本文标题:获取当前view的Controller

      本文链接:https://www.haomeiwen.com/subject/uotivltx.html