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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值