• 主页
  • 系列总集
  • OpenCV
  • CMake
  • iOS
  • Java
  • 前端
所有文章 关于我

  • 主页
  • 系列总集
  • OpenCV
  • CMake
  • iOS
  • Java
  • 前端

获取TopMostViewController

2016-01-08

判断当前窗口是否被加载了

if (self.isViewLoaded && self.view.window ) {
//Do Something
}

获取当前显示的ViewController

因为所有的ViewController都在Window里,而App都是在KeyWindow上,所以可以用以下代码获得

- (UIViewController *)getCurrentVC {
    //1. 先找到KeyWindow
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    //2. 正常情况下KeyWindow应该是在UIWindowLevelNormal,有Alert的时候KeyWindow就是Alert框
    if (window.windowLevel != UIWindowLevelNormal)
    {
        //3. 如果不是UIWindowLevelNormal,那么找到UIWindowLevelNormal级别的Window
        // 这里有个缺陷,因为UIWindowLevelNormal的不一定只有一个,虽然正常情况下只有一个
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                //找到了UIWindowLevelNormal的Window
                window = tmpWin;
                break;
            }
        }
    }
    //4. 判断RootViewController不是TabBarVC和NaviVC,且是ViewController
    id result = window.rootViewController;
    BOOL isViewController = ![result isKindOfClass:[UITabBarController class]] && ![result isKindOfClass:[UINavigationController class]] && [result isKindOfClass:[UIViewController class]];
    //5. 进入递归循环,排除TabBarVC和NaviVC,以及进入PresentedVC继续递归
    while (!isViewController) {
        while ([result isKindOfClass:[UITabBarController class]]) {
            UITabBarController *tempVC = result;
            result = [tempVC selectedViewController];
        }
        while ([result isKindOfClass:[UINavigationController class]]) {
            UINavigationController *tempVC = result;
            result = [tempVC.viewControllers lastObject];
        }
        id presentedVC = [result presentedViewController];
        if (presentedVC) {
            result = presentedVC;
        }
        isViewController = ![result isKindOfClass:[UITabBarController class]] && ![result isKindOfClass:[UINavigationController class]] && [result isKindOfClass:[UIViewController class]];
    }

    return result;
}

该代码是加入了有Presented的VC就查到presented的,如果去掉present检查,可以查到除去model ViewController的第一个

赏

请问老板还招人么(/ω\)

支付宝
微信
  • iOS
  • ViewController
  • Tips

扫一扫,分享到微信

微信分享二维码
ChildViewController产生AlertView偏移的解决
BaseViewController和ViewController+Base的测试
© 2021 Alan Li
Hexo Theme Yilia by Litten
  • 所有文章
  • 关于我

tag:

  • iOS
  • Java
  • Collection
  • Python
  • Shell
  • CMake
  • Memory
  • JavaScript
  • Architecture
  • AnchorPoint
  • Android
  • Web
  • Annotation
  • AFNetworking
  • Window
  • ViewController
  • AutoLayout
  • Dozer
  • CoreAnimation
  • Cycle Retain
  • Block
  • UI
  • IDE
  • FrontEnd
  • CSS
  • Category
  • TableViewCell
  • Security
  • Net
  • JSP
  • Spring
  • C
  • MyBatis
  • Date
  • React
  • GCD
  • UITouch
  • Gesture
  • UIControl
  • Git
  • HTML
  • HTTPS
  • HTTP
  • Servlet
  • Server
  • DataBase
  • MySQL
  • Linux
  • Tutorial
  • Ajax
  • Type
  • JQuery
  • JSON
  • Exception
  • Parameter
  • Reflect
  • Thread
  • Sort
  • KVO
  • MKMapKit
  • Overlay
  • Maven
  • Configure
  • Tips
  • Transaction
  • Swift
  • NavigationBar
  • Nginx
  • Runtime
  • OpenCV
  • Property
  • Playground
  • Protocol
  • Redux
  • ScrollView
  • Session
  • Cookie
  • Shiro
  • Error
  • Singleton
  • RegEx
  • StackView
  • StatusBar
  • Base64
  • Socket
  • TCP
  • IP
  • TextField
  • CALayer
  • UILabel
  • View
  • Animation
  • Xcode
  • Hexo
  • Terminal
  • OC
  • Device
  • Log
  • Image
  • JUnit
  • Oval
  • Archive
  • XSS
  • Compiler
  • Aspect
  • Responder
  • Class
  • FireWall
  • RetainCount
  • Const
  • Frame
  • String
  • Symbols
  • Framework
  • CocoaPods
  • Unity
  • Message
  • Button
  • AuthorizationStatus
  • Struct
  • XCTest
  • NSNotification
  • Contact

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

我写的,大概率是错的。。。。。