Get the top ViewController in iOS Swift
Asked Answered
S

2

7

I want to implement a separate ErrorHandler class, which displays error messages on certain events. The behavior of this class should be called from different other classes. When an error occurs, it will have an UIAlertView as output. The display of this AlertView should ALWAYS be on top. So no matter from where the Error gets thrown, the topmost viewController should display the AlertMessage (e.g. when an asynchronous background process fails, I want an error message, no matter what View is displayed in the foreground).

I have found several gists which seem to solve my problem (see the code below). But calling UIApplication.sharedApplication().keyWindow?.visibleViewController() does return a nil-value.

Extension from gist

extension UIWindow {
func visibleViewController() -> UIViewController? {
if let rootViewController: UIViewController  = self.rootViewController {
  return UIWindow.getVisibleViewControllerFrom(rootViewController)
}
return nil
}

class func getVisibleViewControllerFrom(vc:UIViewController) -> UIViewController {

if vc.isKindOfClass(UINavigationController.self) {

  let navigationController = vc as! UINavigationController
  return UIWindow.getVisibleViewControllerFrom( navigationController.visibleViewController)

} else if vc.isKindOfClass(UITabBarController.self) {

  let tabBarController = vc as! UITabBarController
  return UIWindow.getVisibleViewControllerFrom(tabBarController.selectedViewController!)

} else {

  if let presentedViewController = vc.presentedViewController {

    return UIWindow.getVisibleViewControllerFrom(presentedViewController.presentedViewController!)

  } else {

    return vc;
  }
}
}
}
Submersed answered 1/6, 2015 at 10:14 Comment(1)
To whatever extent that is possible for you, can please you share you're ErrorHandler class on githHub or elsewhere?Aleida
S
25

Amit89 brought a way to a solution up. You have to call the .windowproperty of the AppDelegate. So I changed the Swift code from the link below to work as intended to find the topmost ViewController. Make sure, that the view is already in the view hierarchy. So this method cannot be called from a .viewDidLoad

Extension to find the topmost ViewController*

extension UIApplication {
  class func topViewController(base: UIViewController? = (UIApplication.sharedApplication().delegate as! AppDelegate).window?.rootViewController) -> UIViewController? {
    if let nav = base as? UINavigationController {
      return topViewController(base: nav.visibleViewController)
    }
    if let tab = base as? UITabBarController {
      if let selected = tab.selectedViewController {
        return topViewController(base: selected)
      }
    }
    if let presented = base?.presentedViewController {
      return topViewController(base: presented)
    }
    return base
  }
}

This code originated from GitHub user Yonat in a comment to an objectiveC equivalent. I only changed the bits of code to get it to work without the .keyWindow property

Submersed answered 1/6, 2015 at 11:30 Comment(1)
I am using the your extension to find the top most ViewController, but if alert is presented, the code above gives UIAlertController. How do I get top view controller under UIAlertController?Donau
E
1

Did you try this from the same link?

let appDelegate = UIApplication.sharedApplication().delegate as! MYAppDelegate//Your app delegate class name.


extension UIApplication {
    class func topViewController(base: UIViewController? = appDelegate.window!.rootViewController) -> UIViewController? {
        if let nav = base as? UINavigationController {
            return topViewController(base: nav.visibleViewController)
        }
        if let tab = base as? UITabBarController {
            if let selected = tab.selectedViewController {
                return topViewController(base: selected)
            }
        }
        if let presented = base?.presentedViewController {
            return topViewController(base: presented)
        }
        return base
    }
}
Erlandson answered 1/6, 2015 at 10:53 Comment(2)
Yes I did: fatal error: unexpectedly found nil while unwrapping an Optional value The problem is, that 'UIApplication.sharedApplication().keyWindow?.rootViewController)' returns nilSubmersed
Instead of key window use the window property of app delegate class.Erlandson

© 2022 - 2024 — McMap. All rights reserved.