zoukankan      html  css  js  c++  java
  • IOS获取当前所在的ViewController

    这个主要用于给别人提供的API中,有关跳转的地方。在IOS中,跳转必须提供目前所在的ViewController。代码如下:

    // 获取当前显示的 UIViewController
    + (UIViewController *)dc_findCurrentShowingViewController {
        //获得当前活动窗口的根视图
        UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
        UIViewController *currentShowingVC = [self findCurrentShowingViewControllerFrom:vc];
        return currentShowingVC;
    }
    + (UIViewController *)findCurrentShowingViewControllerFrom:(UIViewController *)vc
    {
        // 递归方法 Recursive method
        UIViewController *currentShowingVC;
        if ([vc presentedViewController]) {
            // 当前视图是被presented出来的
            UIViewController *nextRootVC = [vc presentedViewController];
            currentShowingVC = [self findCurrentShowingViewControllerFrom:nextRootVC];
    
        } else if ([vc isKindOfClass:[UITabBarController class]]) {
            // 根视图为UITabBarController
            UIViewController *nextRootVC = [(UITabBarController *)vc selectedViewController];
            currentShowingVC = [self findCurrentShowingViewControllerFrom:nextRootVC];
    
        } else if ([vc isKindOfClass:[UINavigationController class]]){
            // 根视图为UINavigationController
            UIViewController *nextRootVC = [(UINavigationController *)vc visibleViewController];
            currentShowingVC = [self findCurrentShowingViewControllerFrom:nextRootVC];
    
        } else {
            // 根视图为非导航类
            currentShowingVC = vc;
        }
    
        return currentShowingVC;
    }
    

      

  • 相关阅读:
    杨中科 向HtmlAgilityPack道歉:解析HTML还是你好用
    感觉这个JQuery不错,查询方便
    数据库异步操作
    Command 设计模式
    osg 细节裁剪 SAMLL_FEATURE_CULLING
    errno错误代码
    清空std::stringstream
    eclipse android javabuilder +CDTbuilder
    mfc c++ system调用 控制台窗口
    Androidndkr8e wordlist 第二个参数不是数值参数
  • 原文地址:https://www.cnblogs.com/howlaa/p/13627116.html
Copyright © 2011-2022 走看看