How to get the current displaying UIViewController not in AppDelegate?

杀马特。学长 韩版系。学妹 提交于 2019-12-22 05:35:56

问题


I'm trying to get the currently display UIViewController that is not in the AppDelegate, but it seems to always get the initial top UIViewController, and not the present one.

The following code in AppDelegate DOES get the current UIViewController present, but this same function does not work when I use it in any one of my View Controllers:

func getTopViewController() -> UIViewController
{
    var topViewController = UIApplication.sharedApplication().delegate!.window!!.rootViewController!
    while (topViewController.presentedViewController != nil) {
        topViewController = topViewController.presentedViewController!
    }
    return topViewController
}

The above code was provided as an answer in a similar question: Get the current displaying UIViewController on the screen in AppDelegate.m

No matter how deep down I segue to, I can only retrieve the first-most View Controller.

How can I get the current presenting UIViewController?

FYI: I'm NOT using a UINavigationController, just regular UIViewController classes.


回答1:


I do not like using this but sometimes it is necessary.

static func getTopViewController() -> UIViewController {

    var viewController = UIViewController()

    if let vc =  UIApplication.shared.delegate?.window??.rootViewController {

        viewController = vc
        var presented = vc

        while let top = presented.presentedViewController {
            presented = top
            viewController = top
        }
    }

    return viewController
}

**EDIT:

Here is an improved version, it will always get top most view controller

static var top: UIViewController? {
    get {
        return topViewController()
    }
}

static var root: UIViewController? {
    get {
        return UIApplication.shared.delegate?.window??.rootViewController
    }
}

static func topViewController(from viewController: UIViewController? = UIViewController.root) -> UIViewController? {
    if let tabBarViewController = viewController as? UITabBarController {
        return topViewController(from: tabBarViewController.selectedViewController)
    } else if let navigationController = viewController as? UINavigationController {
        return topViewController(from: navigationController.visibleViewController)
    } else if let presentedViewController = viewController?.presentedViewController {
        return topViewController(from: presentedViewController)
    } else {
        return viewController
    }
}



回答2:


Here is the same idea in one function:

func topController(_ parent:UIViewController? = nil) -> UIViewController {
    if let vc = parent {
        if let tab = vc as? UITabBarController, let selected = tab.selectedViewController {
            return topController(selected)
        } else if let nav = vc as? UINavigationController, let top = nav.topViewController {
            return topController(top)
        } else if let presented = vc.presentedViewController {
            return topController(presented)
        } else {
            return vc
        }
    } else {
        return topController(UIApplication.shared.keyWindow!.rootViewController!)
    }
}

works for me in Swift 4 project



来源:https://stackoverflow.com/questions/41073915/how-to-get-the-current-displaying-uiviewcontroller-not-in-appdelegate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!