Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the current view controller from the app delegate

i am new to ios. I need to know the current view controller from app delegate.. i have no idea about this and i don't knowto implement this. i am using this code toimplemnt this but it return null values. I followed this link- Get current view controller from the app delegate (modal is possible) need help.

like image 670
user3459648 Avatar asked Jul 18 '14 12:07

user3459648


People also ask

How do I present a view controller from another view controller?

You may also start segues from table rows and collection view cells. Right-click the control or object in your current view controller. Drag the cursor to the view controller you want to present. Select the kind of segue you want from the list that Xcode provides.


1 Answers

This is what I use for finding the current view controller that the user is most likely interacting with:

UIViewController+Utils.h

#import <UIKit/UIKit.h>  @interface UIViewController (Utils)  +(UIViewController*) currentViewController;  @end 

UIViewController+Utils.m

#import "UIViewController+Utils.h"  @implementation UIViewController (Utils)  +(UIViewController*) findBestViewController:(UIViewController*)vc {      if (vc.presentedViewController) {          // Return presented view controller         return [UIViewController findBestViewController:vc.presentedViewController];      } else if ([vc isKindOfClass:[UISplitViewController class]]) {          // Return right hand side         UISplitViewController* svc = (UISplitViewController*) vc;         if (svc.viewControllers.count > 0)             return [UIViewController findBestViewController:svc.viewControllers.lastObject];         else             return vc;      } else if ([vc isKindOfClass:[UINavigationController class]]) {          // Return top view         UINavigationController* svc = (UINavigationController*) vc;         if (svc.viewControllers.count > 0)             return [UIViewController findBestViewController:svc.topViewController];         else             return vc;      } else if ([vc isKindOfClass:[UITabBarController class]]) {          // Return visible view         UITabBarController* svc = (UITabBarController*) vc;         if (svc.viewControllers.count > 0)             return [UIViewController findBestViewController:svc.selectedViewController];         else             return vc;      } else {          // Unknown view controller type, return last child view controller         return vc;      }  }  +(UIViewController*) currentViewController {      // Find best view controller     UIViewController* viewController = [UIApplication sharedApplication].keyWindow.rootViewController;     return [UIViewController findBestViewController:viewController];  }  @end 

Then whenever I need the current view controller from anywhere in the app simply use:

[UIViewController currentViewController] 
like image 124
jjv360 Avatar answered Nov 01 '22 13:11

jjv360