Created
January 19, 2015 14:28
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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?