美文网首页
获取当前的control

获取当前的control

作者: 哎呦我去叫什么呢 | 来源:发表于2021-12-21 13:58 被阅读0次

    //获取当前的control

    • (UIViewController *)getCurrentViewController {
      UIViewController *result = nil;
      UIWindow * window = [[UIApplication sharedApplication] keyWindow];
      NSLog(@"window:%@ window.windowLevel:%f",window,window.windowLevel);
      //app默认windowLevel是UIWindowLevelNormal,如果不是,找到它
      if (window.windowLevel != UIWindowLevelNormal) {
      NSLog(@"window:%@ window.windowLevel:%f",window,window.windowLevel);
      NSArray *windows = [[UIApplication sharedApplication] windows];
      for(UIWindow * tmpWin in windows) {
      if (tmpWin.windowLevel == UIWindowLevelNormal) {
      NSLog(@"tmpWin:%@ tmpWin.windowLevel:%f",tmpWin,tmpWin.windowLevel);
      window = tmpWin;
      break;
      }
      }
      }
      id nextResponder = nil;
      UIViewController *appRootVC = window.rootViewController;
      //1、通过present弹出VC,appRootVC.presentedViewController不为nil
      if (appRootVC.presentedViewController) {
      NSLog(@"subviews = %@",[window subviews]);
      nextResponder = appRootVC.presentedViewController;
      }else{
      //2、通过navigationcontroller弹出VC
      NSLog(@"subviews == %@",[window subviews]);
      UIView *frontView = [[window subviews] objectAtIndex:0];
      nextResponder = [frontView nextResponder];
      }
      //1、tabBarController
      if ([nextResponder isKindOfClass:[UITabBarController class]]){

        UITabBarController * tabbar = (UITabBarController *)nextResponder;
        UINavigationController * nav = (UINavigationController *)tabbar.viewControllers[tabbar.selectedIndex];
        //或者 UINavigationController * nav = tabbar.selectedViewController;
        result = nav.childViewControllers.lastObject;
      

      }else if ([nextResponder isKindOfClass:[UINavigationController class]]){
      //2、navigationController

        UIViewController * nav = (UIViewController *)nextResponder;
        result = nav.childViewControllers.lastObject;
      

      }else{//3、viewControler

        result = nextResponder;
      

      }

      return result;
      }

    相关文章

      网友评论

          本文标题:获取当前的control

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