문제

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.

도움이 되었습니까?

해결책

- (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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top