Fraggle
Fraggle

Reputation: 8727

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

In iOS4.2/iPhone4

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.

Upvotes: 7

Views: 9586

Answers (3)

Dan Fabulich
Dan Fabulich

Reputation: 39553

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.

Upvotes: 13

Rob
Rob

Reputation: 520

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.

Upvotes: 0

Thomas Joulin
Thomas Joulin

Reputation: 6650

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

Upvotes: 5

Related Questions