程序退出到后台,控制器的数据还是会存在的,进入到前台时候 不会走viewWillAppear方法了,很多人这里会误导,只有控制器之间来回跳转会进这个;
//哪个控制器需要监听就 写上下面的通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(startMonitor)
name:UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(closeMonitor)
name: UIApplicationDidEnterBackgroundNotification object:nil];
//如果有多个控制器的话 进入到前后和后台的方法会并发一起走
-(void)startMonitor{
if (_timerEveryFive) {
DLog(@"开始监听%@",selectCard);
NSString *str=nil;
if (selectCard.length>0) {
str=selectCard;
}
else{
if (_cardsArray.count>0) {
str=_cardsArray[0][@"cardNo"];
}
}
[self getSuccessInfo:str];
}
}
-(void)closeMonitor{
if (_timerEveryFive) {
DLog(@"关闭监听%@",selectCard);
dispatch_source_cancel(_timerEveryFive);
}
}