Created
September 2, 2016 09:51
-
-
Save DisappearPing/079d5bac3c2b4e0f97a121f96a788162 to your computer and use it in GitHub Desktop.
Find CurrentViewcontroller (or TopViewController) in swift
This file contains 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 AppDelegate { | |
func findCurrentViewController() -> UIViewController{ | |
let rootVC = UIApplication.sharedApplication().keyWindow?.rootViewController | |
return findCurrentViewController(byTempTopVC: rootVC!) | |
} | |
func findCurrentViewController(byTempTopVC vc: UIViewController) -> UIViewController { | |
let presentedVC = vc.presentedViewController | |
guard presentedVC != nil else { | |
return vc | |
} | |
if presentedVC!.isKindOfClass(UINavigationController) { | |
let theNav = presentedVC | |
let theTopVC = theNav!.childViewControllers.last | |
return findCurrentViewController(byTempTopVC: theTopVC!) | |
} | |
return findCurrentViewController(byTempTopVC: presentedVC!) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment