你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> ios 監聽app從後台恢復到前台

ios 監聽app從後台恢復到前台

編輯:IOS開發綜合

正常情況下,在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];

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved