Question

I have only a GalleryViewController as UIViewController.
it's loaded by storyboard.

I'd like to call a method of my GalleryViewController in AppDelegate.m.
But I don't have reference of my GalleryViewController in AppDelegate.m.

Was it helpful?

Solution

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    UIViewController *galleryViewController = self.window.rootViewController;
    [galleryViewController doSomething];
}

Edit: This is just an example of how you will get a reference to your GalleryViewController, if it is your rootViewController. If you are within NavigationController or TabBarController, you then need to first get a reference to your navigationController or tabBarController and then cascade it's viewControllers.

If you have a complex VC structure, it is probably easier to subscribe to notifications as Wain points out. The credit should go to his faster response.

OTHER TIPS

You can get a reference to the root view controller from the window in the app delegate and then cast or navigate the the controller you want (if it's in a navigation controller).

You don't need to add this relationship / knowledge to the app delegate though as the controller can add itself as an observer of the UIApplicationWillEnterForegroundNotification notification.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top