//页面将要进入前台,开启定时器
-(void)viewWillAppear:(BOOL)animated
{
//注册程序进入前台通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (startTimer) name: UIApplicationWillEnterForegroundNotification object:nil];
//注册程序进入后台通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (pauseTimer) name: UIApplicationDidEnterBackgroundNotification object:nil];
[super viewWillAppear:animated];
}
/**
* 这个方法里移除通知
*/
-(void) viewWillDisappear:(BOOL)animated {
//解除程序进入前台通知
[[NSNotificationCenter defaultCenter] removeObserver:self name: UIApplicationWillEnterForegroundNotification object:nil];
//解除程序进入后台通知
[[NSNotificationCenter defaultCenter] removeObserver:self name: UIApplicationDidEnterBackgroundNotification object:nil];
[super viewWillDisappear:animated];
}添加程序进入后台,进入前台的通知
最新推荐文章于 2022-07-26 13:23:27 发布
本文详细介绍了如何在iOS应用中实现程序进入前台和后台的通知,并通过注册和解除通知来管理定时器,确保应用在不同状态下的正确运行。
646

被折叠的 条评论
为什么被折叠?



