當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
- - (void) viewWillAppear:(BOOL)animated{
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillEnterForegroundNotification) name:UIApplicationWillEnterForegroundNotification object:nil];
- }
-
-
- - (void) appWillEnterForegroundNotification{
- NSLog(@"trigger event when will enter foreground.");
- }
不過還有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
- -(void) viewDidDisappear:(BOOL)animated{
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
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
- - (void) viewWillAppear:(BOOL)animated{
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillEnterForegroundNotification) name:UIApplicationWillEnterForegroundNotification object:nil];
- }
-
-
- - (void) appWillEnterForegroundNotification{
- NSLog(@"trigger event when will enter foreground.");
- }
不過還有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
- -(void) viewDidDisappear:(BOOL)animated{
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
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