Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know when a UIViewController view is shown after being in the background?

In iOS4.2/iPhone4

  • Click icon to launch app (some view controllers view is displayed)
  • Click iPhone Home button (return to home screen)
  • double click Home button
  • Select previously launched app from the selection

Now I can see that my app delegate gets a message "applicationDidBecomeActive" when its selected after the last step, but how does my viewController (the one who's view is currently displayed) know?

viewDidLoad was already called, so that isn't called again. viewWillLoad is not called again.

Can't seem to figure it out. Reason I'm asking is I want to check to see if any Settings changes were made, but would like to do that in the view controller cause that's the thing that cares.

like image 560
Fraggle Avatar asked Dec 14 '10 20:12

Fraggle


2 Answers

The answer is here: Handling applicationDidBecomeActive - "How can a view controller respond to the app becoming Active?"

Use NSNotificationCenter to get notified of UIApplicationDidBecomeActiveNotification events in your view controller.

like image 127
Dan Fabulich Avatar answered Sep 19 '22 00:09

Dan Fabulich


in you're appDelegate applicationDidBecomeActive put this :

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    UINavigationController *navc = (UINavigationController *)[tabBarController selectedViewController];

    UIViewController *topvc = [navc topViewController];

    if ([topvc respondsToSelector:@selector(viewControllerDidBecomeActive)]) 
    {
        [topvc performSelector:@selector(viewControllerDidBecomeActive)];
    }
}

This gets the viewController that is being seen on screen. You just have to implement viewControllerDidBecomeActive on every viewControllers ;)

like image 32
Thomas Joulin Avatar answered Sep 19 '22 00:09

Thomas Joulin