When the app switches from background to foreground, it will trigger two methods of appdelegate. M:
ApplicationwillenterforegroundAnd
Applicationdidbecomeactive
But it does not trigger the current view controller'sViewwillappearAndViewdidappearMethod, how to notify current view?
The answer is to register self as the observer of the event in the current view controller!
[CPP]View
Plaincopy
- -(Void) viewwillappear :( bool) animated {
- [[Nsicationcenter center defacenter center] addobserver: Self selector: @ selector (appwillenterforegroundnotification) Name: uiapplicationwillenterforegroundnotification object: Nil];
- }
- -(Void) appwillenterforegroundnotification {
- Nslog (@ "trigger event when will enter foreground .");
- }
However, there are three important issues:
1.You must remove this observer from one of the viewwilldisappear/viewdiddisappear methods of the View Controller.. This is because, if you switch from the current view to another view and return to this view, it will be registered as an observer again. In this way, when you go from bgground to foreground, it will trigger the observer method twice!
Remove observer example:
[CPP]View
Plaincopy
- -(Void) viewdiddisappear :( bool) animated {
- [[Nsicationcenter center defacenter center] removeobserver: Self];
- }
2. Switch from view a to view B, and both views have registered the observer from BG to FG. when an event occurs, the trigger method of both views will be called. When View B is closed (destroy), only view a is Trigger.
3. Why not add observer in didload? But sometimes it is not suitable. For example, view a is a root view, and add observer in didload method. Since switching to another view will remove observer, when view a is returned, didload will no longer be called to add observer.
For example, see willenterforegroundnotificationdemo project.
Rel links:
Http://stackoverflow.com/questions/3771015/what-method-is-called-when-application-appears-from-background-on-iphone
Http://stackoverflow.com/questions/3445268/respond-to-applicationwillenterforeground-in-a-uiview
Http://stackoverflow.com/questions/6103616/alter-view-before-applicationwillenterforeground
Http://stackoverflow.com/questions/7569187/changing-uiview-when-applicationwillenterforeground-fires
But it does not trigger the current view controller'sViewwillappearAndViewdidappearMethod, how to notify current view?
The answer is to register self as the observer of the event in the current view controller!
[CPP]View
Plaincopy
- -(Void) viewwillappear :( bool) animated {
- [[Nsicationcenter center defacenter center] addobserver: Self selector: @ selector (appwillenterforegroundnotification) Name: uiapplicationwillenterforegroundnotification object: Nil];
- }
- -(Void) appwillenterforegroundnotification {
- Nslog (@ "trigger event when will enter foreground .");
- }
However, there are three important issues:
1.You must remove this observer from one of the viewwilldisappear/viewdiddisappear methods of the View Controller.. This is because, if you switch from the current view to another view and return to this view, it will be registered as an observer again. In this way, when you go from bgground to foreground, it will trigger the observer method twice!
Remove observer example:
[CPP]View
Plaincopy
- -(Void) viewdiddisappear :( bool) animated {
- [[Nsicationcenter center defacenter center] removeobserver: Self];
- }
2. Switch from view a to view B, and both views have registered the observer from BG to FG. when an event occurs, the trigger method of both views will be called. When View B is closed (destroy), only view a is Trigger.
3. Why not add observer in didload? But sometimes it is not suitable. For example, view a is a root view, and add observer in didload method. Since switching to another view will remove observer, when view a is returned, didload will no longer be called to add observer.
For example, see willenterforegroundnotificationdemo project.
Rel links:
Http://stackoverflow.com/questions/3771015/what-method-is-called-when-application-appears-from-background-on-iphone
Http://stackoverflow.com/questions/3445268/respond-to-applicationwillenterforeground-in-a-uiview
Http://stackoverflow.com/questions/6103616/alter-view-before-applicationwillenterforeground
Http://stackoverflow.com/questions/7569187/changing-uiview-when-applicationwillenterforeground-fires