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 开发者_StackOverflow社区view controller cause that's the thing that cares.
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.
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 ;)
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.
精彩评论