I need an instance of root view controller.
I tried those approaches:
UIViewController *rootViewController = (UIViewController*)[[[UIApplication sharedApplication] keyWindow] rootViewController];
Returns: null:
Also when I try to get an array of controllers:
NSArray *viewControllers = self.navigationController.viewControllers;
It returns only one controller, but it isn't my root view controller.
If I try to take from navigation controller:
UIViewController *root = (UIViewController*)[self.navigationController.viewControllers objectAtIndex:0];
Returns: null:
Any ideas why? What else could I try to get an instance of my root view controller?
Thanks.
In short hand way of access root view controller.
Objective C
Swift 2.0
Swift 3
let rootViewController = UIApplication.shared.keyWindow?.rootViewController
Unless you have a good reason, in your root controller do this:
And when you want to notify it: