iOS: 当app从background切换到foreground,如何通知uiviewcontroller


当app从background切换到foreground,会trigger AppDelegate.m的2个方法:
applicationWillEnterForeground and applicationDidBecomeActive

但却不会trigger current view controller的viewWillAppearandviewDidAppear方法,那么怎么通知current view呢?

答案是通过在current view controller里把self注册为该event的observer!

  1. -(void)viewWillAppear:(BOOL)animated{
  2. [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(appWillEnterForegroundNotification)name:UIApplicationWillEnterForegroundNotificationobject:nil];
  3. }
  4. -(void)appWillEnterForegroundNotification{
  5. NSLog(@"triggereventwhenwillenterforeground.");
  6. }

不过还有3个重要的issues:

1.必须在view controller的 viewWillDisappear/viewDidDisappear 其中一个方法中remove this observer。这是因为假设从当前view切换到另一个view再返回到该view,又会注册一次为observer,这样当从bgground到foreground时就会trigger 2次observer的方法!

remove observer example:

  1. -(void)viewDidDisappear:(BOOL)animated{
  2. [[NSNotificationCenterdefaultCenter]removeObserver:self];
  3. }


2. 从view A切换到view B,2个view都注册了from bg to fg的observer,那么当event发生时,2个view的trigger method会都被调用。而当view B close (即被destroy)后,则只会trigger view A的。


3.为什么不在didLoad里add observer?其实是可以的,但有时不适合。例如,view A是root view,在didLoad method里add observer,由于切换到其他view会remove observer,那么当再返回view A时则不会再调用didLoad来add observer。


例子参考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


但却不会trigger current view controller的viewWillAppearandviewDidAppear方法,那么怎么通知current view呢?

答案是通过在current view controller里把self注册为该event的observer!

  1. -(void)viewWillAppear:(BOOL)animated{
  2. [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(appWillEnterForegroundNotification)name:UIApplicationWillEnterForegroundNotificationobject:nil];
  3. }
  4. -(void)appWillEnterForegroundNotification{
  5. NSLog(@"triggereventwhenwillenterforeground.");
  6. }

不过还有3个重要的issues:

1.必须在view controller的 viewWillDisappear/viewDidDisappear 其中一个方法中remove this observer。这是因为假设从当前view切换到另一个view再返回到该view,又会注册一次为observer,这样当从bgground到foreground时就会trigger 2次observer的方法!

remove observer example:

  1. -(void)viewDidDisappear:(BOOL)animated{
  2. [[NSNotificationCenterdefaultCenter]removeObserver:self];
  3. }


2. 从view A切换到view B,2个view都注册了from bg to fg的observer,那么当event发生时,2个view的trigger method会都被调用。而当view B close (即被destroy)后,则只会trigger view A的。


3.为什么不在didLoad里add observer?其实是可以的,但有时不适合。例如,view A是root view,在didLoad method里add observer,由于切换到其他view会remove observer,那么当再返回view A时则不会再调用didLoad来add observer。


例子参考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


你可能感兴趣的:(controller)