Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current displaying UIViewController not in AppDelegate?

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.

like image 222
Pangu Avatar asked Dec 10 '16 09:12

Pangu


2 Answers

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
    }
}
like image 93
Markicevic Avatar answered Oct 19 '22 15:10

Markicevic


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

like image 44
Paul Zabelin Avatar answered Oct 19 '22 15:10

Paul Zabelin