Skip to content

Instantly share code, notes, and snippets.

@db0company
Created January 19, 2015 14:28
Show Gist options
  • Star 55 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save db0company/369bfa43cb84b145dfd8 to your computer and use it in GitHub Desktop.
Save db0company/369bfa43cb84b145dfd8 to your computer and use it in GitHub Desktop.
Get the top most viewController anywhere in the app (typically from the appDelegate). Get the current visible viewController.
extension UIViewController {
func topMostViewController() -> UIViewController {
if self.presentedViewController == nil {
return self
}
if let navigation = self.presentedViewController as? UINavigationController {
return navigation.visibleViewController.topMostViewController()
}
if let tab = self.presentedViewController as? UITabBarController {
if let selectedTab = tab.selectedViewController {
return selectedTab.topMostViewController()
}
return tab.topMostViewController()
}
return self.presentedViewController!.topMostViewController()
}
}
extension UIApplication {
func topMostViewController() -> UIViewController? {
return self.keyWindow?.rootViewController?.topMostViewController()
}
}
@AMBIENTE1
Copy link

@ielyas please, your code work with iPhone fine, but on iPad it returns vc : Fatal error: Unexpectedly found nil while unwrapping an Optional value
Any idea what's different for the iPad?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment