6

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.

Fraggle
  • 8,607
  • 7
  • 54
  • 86

3 Answers3

12

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.

Community
  • 1
  • 1
Dan Fabulich
  • 37,506
  • 41
  • 139
  • 175
  • @fraggle This looks like the most reliable solution. Please consider marking it as the answer to your question. – ettore Jul 25 '12 at 19:51
5

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 ;)

Thomas Joulin
  • 6,590
  • 9
  • 53
  • 88
  • Right, that sort of works, but not exactly what I was looking for. My view hierarchy is a bit more complex, so your posted code won't work as is, but I get the idea and I could do it that way. So its definitely one way to go. I guess I was hoping I missed something and there was a more straightforward way that was independent of whatever viewcontroller hierarchy one has. – Fraggle Dec 14 '10 at 20:58
  • Several years later now, but AFAICS the best option would be to use NSNotificationCenter. – Reuben Scratton Oct 15 '13 at 12:18
0

In the appDelegate applicationDidBecomeActive set a boolean property marking that it just appeared from background.

Then in your viewcontroller, specifically in the viewDidAppear override, check for the appDelegate property, if its true then you know it has come from the background, otherwise it has just appeared as normal. BTW Afterwards, set the boolean property to false for neatness.

EDIT- You would have to call viewDidAppear manually in the applicationDidBecomeActive unless you were re-creating your navigation stack. If you were able to get a pointer to the current visible view controller, then calling viewDidAppear should be a no fuss approach as all view controllers have this method. You wouldn't need any delegates or etc.

Rob
  • 520
  • 5
  • 17
  • Well no, that doesn't do it. The view has already appeared. Then someone goes and changes some settings, then they go back into the app. Typically on iOS 4 anyway, the viewDidAppear will not get called again, right? – Fraggle Dec 15 '10 at 19:55
  • Ah sorry your right! I forgot in my apps I'm doing things to the navigation stack in applicationDidBecomeActive. If you have a pointer to your current visible view controller, then you could just call a function inside your view telling it to check if anything changed, but that's essentially the same as Thomas's answer, but without the elegance of delegates. – Rob Dec 16 '10 at 07:04