在ApplicationWillResignActive视图控制器变量 [英] view controller variables in ApplicationWillResignActive

查看:121
本文介绍了在ApplicationWillResignActive视图控制器变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过节省时间磁盘上进入的背景和检索它在进入前景,使在后台我的定时器运行。每个视图控制器有一个计时器和由用户指定的时间间隔内。问题是,我不知道如何访问一个时间间隔变量。我想我可以用这样的事情得到的时间差(将这项工作?):

I'm trying to make my timer "run" in the background by saving the time to disk on entering background and retrieving it on entering foreground. Each view controller has a timer and a timeInterval specified by the user. The problem is, I don't know how to access the timeInterval variable. I think I can get the difference of time by using something like this (would this work?):

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

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

Each view controller (and timer/time interval) is accessed like this:

-(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];
}

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

I am adding each view controller to the Notification Center so it can be accessed in the app delegate (i think this is right?). Problem is, I don't know how to combine the first code with the view controller code, because I can't access the view controller's properties in the app delegate. Any suggestions so that I can make my timer "run" in the background?

推荐答案

您要对此都错了。有没有必要做任何这在应用程序的委托。

You are going about this all wrong. There is no need to do any of this in the app delegate.

让每个视图控制器监听 UIApplicationWillResignActiveNotification 的通知。然后,每个视图控制器可以做任何它认为合适接到通知时保存其数据。

Have each view controller listen for the UIApplicationWillResignActiveNotification notification. Then each view controller can do whatever it feels is appropriate to save its data when the notification is received.

更新:

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

In the view controller's init... method, register for the notification:

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

在视图控制器的的dealloc 方法,注销:

In the view controller's dealloc method, unregister:

- (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
}

这篇关于在ApplicationWillResignActive视图控制器变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆