Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get visible viewController from app delegate when using storyboard?

Tags:

ios

I have some viewControllers, and I don't use NavigationController. How can I get visible view controller in app delegate methods (e.g. applicationWillResignActive)?

I know how to do it from NSNotification, but I think it's the wrong way.

like image 755
user3501341 Avatar asked Apr 05 '14 14:04

user3501341


People also ask

How do I embed a view controller in navigation controller storyboard?

In your storyboard, select the initial view controller in your hierarchy. With this view controller selected, choose the menu item Editor -> Embed In -> Navigation Controller .

How do I change the initial view controller in storyboard?

Specifying the Initial View Controllerstoryboard and select the Tab Bar Controller Scene. On the right, select the Attribute inspector. You'll find a checkbox named Is Initial View Controller. Checking this box will identify the selected view controller as the initial entry point for the storyboard you're on.


1 Answers

This should do it for you:

- (void)applicationWillResignActive:(UIApplication *)application {     UIViewController *vc = [self visibleViewController:[UIApplication sharedApplication].keyWindow.rootViewController]; }  - (UIViewController *)visibleViewController:(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 visibleViewController:lastViewController];     }     if ([rootViewController.presentedViewController isKindOfClass:[UITabBarController class]])     {         UITabBarController *tabBarController = (UITabBarController *)rootViewController.presentedViewController;         UIViewController *selectedViewController = tabBarController.selectedViewController;          return [self visibleViewController:selectedViewController];     }      UIViewController *presentedViewController = (UIViewController *)rootViewController.presentedViewController;      return [self visibleViewController:presentedViewController]; } 
like image 127
klcjr89 Avatar answered Sep 23 '22 20:09

klcjr89