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];


发布了283 篇原创文章 · 获赞 53 · 访问量 51万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览