正常情況下,在AppDelegate中實現下面兩個方法,能夠監聽從後台恢復到前台
[cpp]
- (void)applicationDidEnterBackground:(UIApplication *)application
{
log4info(@"---applicationDidEnterBackground----");
//進入後台
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
NSLog(@"---applicationDidBecomeActive----");
//進入前台
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
log4info(@"---applicationDidEnterBackground----");
//進入後台
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
NSLog(@"---applicationDidBecomeActive----");
//進入前台
}
但是單個UIViewController怎麼監聽呢
在-viewDidLoad方法中,添加一下代碼監聽notification
[cpp]
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(someMethod:)
name:UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(someMethod:)
name:UIApplicationDidBecomeActiveNotification object:nil];
別忘了在-dealloc中將監聽移除:
[cpp]
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] removeObserver:self];