iOS Developer

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

2016-03-23  本文已影响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];

}
上一篇 下一篇

猜你喜欢

热点阅读