查看ApplicationWillResignActive中的控制器variables

我试图让我的计时器在后台运行,通过在进入背景时保存磁盘的时间,并在进入前台时检索它。 每个视图控制器都有一个定时器和一个由用户指定的timeInterval。 问题是,我不知道如何访问timeIntervalvariables。 我想我可以通过使用这样的东西来获得时间的差异(这会工作吗?):

NSTimeInterval idleTime = [dateReturnedToForeground timeIntervalSinceDate:dateEnteredBackground]; NSTimeInterval elapsedTime = [[NSDate date] timeIntervalSinceDate:startDate]; elapsedTime -= idleTime; 

每个视图控制器(和定时器/时间间隔)是这样访问的:

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ DetailViewController *detailVC; if (![self.detailViewsDictionary.allKeys containsObject:indexPath]){ detailVC = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil]; [self.detailViewsDictionary setObject:detailVC forKey:indexPath]; detailVC.context = self.managedObjectContext; }else{ detailVC = self.detailViewsDictionary[indexPath]; } Tasks *task = [[self fetchedResultsController] objectAtIndexPath:indexPath]; detailVC.testTask = task; [[self navigationController] pushViewController:detailVC animated:YES]; NSLog(@"%@", self.detailViewsDictionary); [[NSNotificationCenter defaultCenter] addObserver:detailVC forKeyPath:self.detailViewsDictionary[indexPath] options:nil context:nil]; } 

我将每个视图控制器添加到通知中心,所以它可以在应用程序委托(我认为这是正确的?)访问。 问题是,我不知道如何将第一个代码与视图控制器代码结合起来,因为我无法访问应用程序委托中的视图控制器的属性。 任何build议,以便我可以让我的计时器在后台运行?

你一切都错了。 在应用程序委托中不需要做任何这样的事情。

让每个视图控制器侦听UIApplicationWillResignActiveNotification通知。 然后,每个视图控制器可以做任何感觉是适当的,当收到通知时保存其数据。

更新:

在视图控制器的init...方法中,注册通知:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resigningActive) name:UIApplicationWillResignActiveNotification object:nil]; 

在视图控制器的dealloc方法中,取消注册:

 - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil]; } 

然后执行resigningActive方法:

 - (void)resigningActive { // The app is resigning active - do whatever this view controller needs to do }