How do I access my viewController from my appDelegate? iOS
Solution 1:
You can access it with:
MyViewController* mainController = (MyViewController*) self.window.rootViewController;
If you are nesting your view behind a tabviewcontroller or navigation controller it will return that to you and you will need to access your view controller inside of it
Solution 2:
How about using good old fashioned NSNotifications to send a message from your app delegate to anyone listening (e.g. your view contorller) that something needs to be updated? or you can use Key Value Observing so your view controller can be watching some property in your app delegate.
Solution 3:
Since you only have one view controller, the generic way (independent of how your app was set up):
UIViewController *vc = [[[UIApplication sharedApplication] keyWindow] rootViewController];
Solution 4:
If you want to get any other UIViewController
, not just the rootViewController
:
UIWindow *window=[UIApplication sharedApplication].keyWindow;
UIViewController *root = [window rootViewController];
UIStoryboard *storyboard = root.storyboard;
CustomViewController *vcc =(CustomViewController *) [storyboard instantiateViewControllerWithIdentifier:@"storyBoardID"];