美文网首页iOS Developer
当APP从后台返回到前台的时候,ViewController该怎

当APP从后台返回到前台的时候,ViewController该怎

作者: 司机王 | 来源:发表于2016-03-23 16:59 被阅读4932次

今天遇到个需求,当app从前台调到后台时,再从后台调回前台时,要处理一些业务逻辑,我觉得很简单,如果App的重新出现在界面,那肯定会调用ViewController的- (void)viewWillAppear:(BOOL)animated方法。道理很简单,不就是跟vc的进栈出栈一样吗?于是把一些操作写在上,运行起来,发现并没有我们想要的效果。

后来在网上查了一下,发现这样并不会调用viewWillAppear这个方法

那怎么办,其实很简单,iOS已经帮我们做好了一些事,只要我们注册监听一下就好了

上代码

- (void)viewWillAppear:(BOOL)animated{
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground)name:UIApplicationWillEnterForegroundNotification object:nil];

   }
- (void)applicationWillEnterForeground{

    //do u want to do
}

这样就能在VC中监听到app从后台回到前台了。

最后一定别忘了要remove这个监听者

- (void)viewDidDisappear:(BOOL)animated{

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

相关文章

网友评论

    本文标题:当APP从后台返回到前台的时候,ViewController该怎

    本文链接:https://www.haomeiwen.com/subject/naollttx.html