IOS: How to notify uiviewcontroller when the app switches from background to foreground?

Source: Internet
Author: User

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

  1. -(Void) viewwillappear :( bool) animated {
  2. [[Nsicationcenter center defacenter center] addobserver: Self selector: @ selector (appwillenterforegroundnotification) Name: uiapplicationwillenterforegroundnotification object: Nil];
  3. }
  4. -(Void) appwillenterforegroundnotification {
  5. Nslog (@ "trigger event when will enter foreground .");
  6. }


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

  1. -(Void) viewdiddisappear :( bool) animated {
  2. [[Nsicationcenter center defacenter center] removeobserver: Self];
  3. }



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

  1. -(Void) viewwillappear :( bool) animated {
  2. [[Nsicationcenter center defacenter center] addobserver: Self selector: @ selector (appwillenterforegroundnotification) Name: uiapplicationwillenterforegroundnotification object: Nil];
  3. }
  4. -(Void) appwillenterforegroundnotification {
  5. Nslog (@ "trigger event when will enter foreground .");
  6. }


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

  1. -(Void) viewdiddisappear :( bool) animated {
  2. [[Nsicationcenter center defacenter center] removeobserver: Self];
  3. }



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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.