• ios 获取当前ViewController


    - (UIViewController*)getpresentVC:(UIWindow *)window{
        if ([window.rootViewController class]==[UITabBarController class]) {
            UITabBarController *tabBarController = (UITabBarController*)window.rootViewController;
            UINavigationController *navi = [tabBarController.viewControllers lastObject];
            NSLog(@"%d",navi.viewControllers.count);
            UIViewController *VC =[navi.viewControllers lastObject];
            if (!VC.presentedViewController) {
                return VC.presentedViewController;
            }else{
                return VC;
            }
        }else{
            return window.rootViewController;
        }
    }

     或者使用递归调用

    - (UIViewController*)topViewController {
        return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
    }
    - (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {
        
        if ([rootViewController isKindOfClass:[UITabBarController class]]) {
            
            UITabBarController* tabBarController = (UITabBarController*)rootViewController;
            return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
        } else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
            UINavigationController* nav = (UINavigationController*)rootViewController;
            return [self topViewControllerWithRootViewController:nav.visibleViewController];
        } else if (rootViewController.presentedViewController) {
            UIViewController* presentedViewController = rootViewController.presentedViewController;
            return [self topViewControllerWithRootViewController:presentedViewController];
        } else {
            return rootViewController;
        }
    }
  • 相关阅读:
    Stream 常规操作
    Stream Introduction
    那些从阿里巴巴走出的创业牛人们
    码农转型传统行业更容易成功?
    创业者怎么讲故事打动投资人?
    李明远:移动互联网的创业时代
    想当年,那些抄我们试卷的坏分子,如今个个都当了老板.
    为什么说淘宝创业已难赚钱?
    让低版本IE支持Html5的新语义标签
    译文:TypeScript新手指南
  • 原文地址:https://www.cnblogs.com/chaochaobuhuifei55/p/5553790.html
Copyright © 2020-2023  润新知