当前位置:编程学习 > wap >>

ios 监听app从后台恢复到前台

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

 

补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,