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

获取当前控制器

作者: Buddha_like | 来源:发表于2017-08-24 18:58 被阅读0次

    func getCurrentController() -> UIViewController? {

    return topViewControllerWithRootViewController(viewController:getCurrentWindow()?.rootViewController)

    }

    func topViewController() -> UIViewController? {

    return topViewControllerWithRootViewController(viewController:getCurrentController())

    }

    func topViewControllerWithRootViewController(viewController :UIViewController?) -> UIViewController? {

    if viewController == nil {return nil}

    if viewController?.presentedViewController != nil {

    return topViewControllerWithRootViewController(viewController: viewController?.presentedViewController!)

    }else if viewController?.isKind(of: UITabBarController.self) == true {

    return topViewControllerWithRootViewController(viewController: (viewController as! UITabBarController).selectedViewController)

    }else if viewController?.isKind(of: UINavigationController.self) == true {

    return topViewControllerWithRootViewController(viewController: (viewController as! UINavigationController).visibleViewController)

    }else{

    return viewController

    }

    }

    func getCurrentWindow() -> UIWindow? {

    var window: UIWindow? = UIApplication.shared.keyWindow

    if window?.windowLevel != UIWindowLevelNormal {

    for tempWindow in UIApplication.shared.windows {

    if tempWindow.windowLevel == UIWindowLevelNormal {

    window = tempWindow

    break

    }

    }

    }

    return window

    }

    原文转自:http://www.cnblogs.com/SoulKai/p/6278373.html

    相关文章

      网友评论

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

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