Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIApplicationWillEnterForegroundNotification versus viewWillAppear - pattern for initialization?

Tags:

iphone

ios4

I learned my app does not behave well on multitasking iOS in case a certain ViewController was active when quitting the app. This view controller needs to gather recent information in order to display it. So far initialization code for gathering this information was only placed in viewWillAppear.

Now I've learned viewWillAppear will not be called when the view will appear from background.

Since the world may have changed since the app was send to the background, the view controller needs to reinitialize. I understand I may register for UIApplicationWillEnterForegroundNotification. However, this fires every time the app comes back to the foreground, no matter whether the user is currently navigating to this particular viewController.

Do you have a pattern how to make use of UIApplicationWillEnterForegroundNotification only when the ViewController is active in the sense of "will appear shortly"?

like image 922
Miriam Avatar asked Jan 05 '11 17:01

Miriam


1 Answers

Register for UIApplicationWillEnterForegroundNotification in viewWillAppear:, and unregister in viewWillDisappear:. That way you'll only be notified when the view is actually visible.

like image 123
Daniel Dickison Avatar answered Nov 13 '22 13:11

Daniel Dickison