美文网首页
Swift - 获取当前的ViewController

Swift - 获取当前的ViewController

作者: 麦志超 | 来源:发表于2017-09-20 10:06 被阅读0次

    转自:http://www.jianshu.com/p/30b341d5edfa


    // 获取当前显示的ViewController

    extension UIViewController
    {
        // 获取当前显示的ViewController
        class func currentViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController?
        {
            if let nav = base as? UINavigationController
            {
                 return currentViewController(base: nav.visibleViewController)
            }
            if let tab = base as? UITabBarController
            {
                 return currentViewController(base: tab.selectedViewController)
            }
            if let presented = base?.presentedViewController
            {
                 return currentViewController(base: presented)
            }
            return base
        }
    }
    
    
    获取当前VC:
    
    let theViewControllerYouSee = UIViewController.currentViewController()
    

    PS:转载仅为了学习使用,如有不妥,请联系,即删。

    相关文章

      网友评论

          本文标题:Swift - 获取当前的ViewController

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