美文网首页收藏swift
swift 获取当前控制器

swift 获取当前控制器

作者: chinwy | 来源:发表于2017-07-21 15:58 被阅读485次
        func getCurrentController() -> UIViewController? {
            guard let window = UIApplication.shared.windows.first else {
                return nil
            }
            var tempView: UIView?
            for subview in window.subviews.reversed() {
                if subview.classForCoder.description() == "UILayoutContainerView" {
                    tempView = subview
                    break
                }
            }
            
            if tempView == nil {
                tempView = window.subviews.last
            }
            
            var nextResponder = tempView?.next
            var next: Bool {
                return !(nextResponder is UIViewController) || nextResponder is UINavigationController || nextResponder is UITabBarController
            }
    
            while next{
                tempView = tempView?.subviews.first
                if tempView == nil {
                    return nil
                }
                nextResponder = tempView!.next
            }
            return nextResponder as? UIViewController
        }
    

    相关文章

      网友评论

        本文标题:swift 获取当前控制器

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