Skip to content

Instantly share code, notes, and snippets.

@DisappearPing
Created September 2, 2016 09:51
Show Gist options
  • Save DisappearPing/079d5bac3c2b4e0f97a121f96a788162 to your computer and use it in GitHub Desktop.
Save DisappearPing/079d5bac3c2b4e0f97a121f96a788162 to your computer and use it in GitHub Desktop.
Find CurrentViewcontroller (or TopViewController) in swift
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