Current view controller from AppDelegate?

Is there a way to get the current view controller from AppDelegate? I know there is a rootViewController, but that is not what I am looking for.

+41
ios objective-c uiviewcontroller
Jan 25 '12 at 20:16
source share
9 answers

If you have a UINavigationController in appDelegate then use its topViewController or visibleViewController property

+19
Jan 25 '12 at 20:21
source share
— -

If your root application view manager is a UINavigationController , you can do this:

 ((UINavigationController*)appDelegate.window.rootViewController).visibleViewController; 

Similarly, if it is a UITabBarController , you can do this:

 ((UITabBarController*)appDelegate.window.rootViewController).selectedViewController; 

Of course, explicit casting like this is dirty. It would be better to capture the link yourself using strong types.

+55
Sep 03 '13 at 17:41
source share

This can help

 - (UIViewController *)topViewController{ return [self topViewController:[UIApplication sharedApplication].keyWindow.rootViewController]; } - (UIViewController *)topViewController:(UIViewController *)rootViewController { if (rootViewController.presentedViewController == nil) { return rootViewController; } if ([rootViewController.presentedViewController isKindOfClass:[UINavigationController class]]) { UINavigationController *navigationController = (UINavigationController *)rootViewController.presentedViewController; UIViewController *lastViewController = [[navigationController viewControllers] lastObject]; return [self topViewController:lastViewController]; } UIViewController *presentedViewController = (UIViewController *)rootViewController.presentedViewController; return [self topViewController:presentedViewController]; } 

Quick version:

 extension UIApplication { class func topViewController(base: UIViewController? = UIApplication.sharedApplication().keyWindow?.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 } } 

Taken from: https://gist.github.com/snikch/3661188

+21
Jun 17 '15 at 2:16
source share

Get the appDelegate object:

 MyAppDelegate *tmpDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate]; 

As beryllium suggested, you can use the UINavigationController properties to access your current controller.

So, the code will look like this:

 id myCurrentController = tmpDelegate.myNavigationController.topViewController; 

or

 NSArray *myCurrentViewControllers = tmpDelegate.myNavigationController.viewControllers; 
+10
Jan 25 '12 at 20:50
source share

Make an extension:

 extension UIApplication { class func topViewController(base: UIViewController? = UIApplication.sharedApplication().keyWindow?.rootViewController) -> UIViewController? { if let nav = base as? UINavigationController { return topViewController(nav.visibleViewController) } if let tab = base as? UITabBarController { let moreNavigationController = tab.moreNavigationController if let top = moreNavigationController.topViewController where top.view.window != nil { return topViewController(top) } else if let selected = tab.selectedViewController { return topViewController(selected) } } if let presented = base?.presentedViewController { return topViewController(presented) } return base } } 

Application:

 if let rootViewController = UIApplication.topViewController() { //do sth with root view controller } 
+10
Feb 11 '16 at 12:13
source share

You can get the current view controller from rootViewController by searching for the presented ViewController, for example:

 UIViewController *parentViewController = [[[UIApplication sharedApplication] delegate] window].rootViewController; while (parentViewController.presentedViewController != nil){ parentViewController = parentViewController.presentedViewController; } UIViewController *currentViewController = parentViewController; 

He works with me. Hope this helps :)

+8
Jul 13 '15 at 3:47
source share

For those who do not use the UINavigationController , and their default view controller is the UIViewController , you can check which view controller is active (or represented) in AppDelegate :

 func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int { if let rootViewController = self.window!.rootViewController { if let presentedViewController = rootViewController.presentedViewController { return presentedViewController.supportedInterfaceOrientations() } } // Else current view controller is DefaultViewController return Int(UIInterfaceOrientationMask.Portrait.rawValue) } 

As you can see, I am checking the current view controller to support different interface orientations for specific controllers. For anyone interested in using this method to support specific ones, each view controller that needs a certain orientation should have the following:

 override func supportedInterfaceOrientations() -> Int { return Int(UIInterfaceOrientationMask.All.rawValue) } 

Note: This code was written using Swift 1.2.

+4
Apr 30 '15 at 19:46
source share

Fast decision:

  self.window.rootViewController.presentedViewController. 

This should get what you need.

+4
Aug 21 '15 at 15:14
source share

Often I need to get the display controller that is currently being displayed. This may mean the view controller at the top of the stack of the current UINavigationController, the current view controller presented, etc. Therefore, I wrote this function, which shows it most of the time, and that you can use inside the UIViewController extension.

Code in Swift 3 :

 func currentViewController( _ viewController: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? { guard let viewController = viewController else { return nil } if let viewController = viewController as? UINavigationController { if let viewController = viewController.visibleViewController { return currentViewController(viewController) } else { return currentViewController( viewController.topViewController) } } else if let viewController = viewController as? UITabBarController { if let viewControllers = viewController.viewControllers, viewControllers.count > 5, viewController.selectedIndex >= 4 { return currentViewController( viewController.moreNavigationController) } else { return currentViewController( viewController.selectedViewController) } } else if let viewController = viewController.presentedViewController { return viewController } else if viewController.childViewControllers.count > 0 { return viewController.childViewControllers[0] } else { return viewController } } 

Name it with currentViewController()

0
Sep 13 '17 at 22:38 on
source share



All Articles