iOS: 當app從background切換到foreground,如何通知uiviewcontroller

來源:互聯網
上載者:User

當app從background切換到foreground,會trigger AppDelegate.m的2個方法: applicationWillEnterForeground and  applicationDidBecomeActive

但卻不會trigger current view controller的 viewWillAppear and viewDidAppear 方法,那麼怎麼通知current view呢?

答案是通過在current view controller裡把self註冊為該event的observer!

[cpp] view
plaincopy

  1. - (void) viewWillAppear:(BOOL)animated{  
  2.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillEnterForegroundNotification) name:UIApplicationWillEnterForegroundNotification object:nil];  
  3. }  
  4.   
  5.   
  6. - (void) appWillEnterForegroundNotification{  
  7.     NSLog(@"trigger event when will enter foreground.");  
  8. }  


不過還有3個重要的issues:

1. 必須在view controller的 viewWillDisappear/viewDidDisappear 其中一個方法中remove this observer。這是因為假設從當前view切換到另一個view再返回到該view,又會註冊一次為observer,這樣當從bgground到foreground時就會trigger 2次observer的方法!

remove observer example:

[cpp] view
plaincopy

  1. -(void) viewDidDisappear:(BOOL)animated{  
  2.     [[NSNotificationCenter defaultCenter] 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的 viewWillAppear and viewDidAppear 方法,那麼怎麼通知current view呢?

答案是通過在current view controller裡把self註冊為該event的observer!

[cpp] view
plaincopy

  1. - (void) viewWillAppear:(BOOL)animated{  
  2.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillEnterForegroundNotification) name:UIApplicationWillEnterForegroundNotification object:nil];  
  3. }  
  4.   
  5.   
  6. - (void) appWillEnterForegroundNotification{  
  7.     NSLog(@"trigger event when will enter foreground.");  
  8. }  


不過還有3個重要的issues:

1. 必須在view controller的 viewWillDisappear/viewDidDisappear 其中一個方法中remove this observer。這是因為假設從當前view切換到另一個view再返回到該view,又會註冊一次為observer,這樣當從bgground到foreground時就會trigger 2次observer的方法!

remove observer example:

[cpp] view
plaincopy

  1. -(void) viewDidDisappear:(BOOL)animated{  
  2.     [[NSNotificationCenter defaultCenter] 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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.