zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    初识Java,关于一个简单的ATM机的java程序设计
    字符串和字符串对象的区别
    集中常见得字符串处理方式
    得到类模板的3种方式
    反射的条件
    封装一个标签加文本框
    建立及中常见的布局管理器
    随机输入3个正整数,程序出来从小到大排列
    java 基础
    IO
  • 原文地址:https://www.cnblogs.com/chaochaobuhuifei55/p/5553790.html
Copyright © 2011-2022 走看看