在我的“xcode”计时器项目中,我有一个名为“overall”的int变量。当我第一次设置我的计时器时,整个变量被设置为计时器所需的秒数。然后我每隔一段NSTimer
取一个变量。
overall = (timerMin * 60) + (timerHour * 60 * 60) + timer;
[self setTimer];
When i first set the variable
如果应用程序进入后台,我会记录日期,然后记录进入前台的日期。然后我找出两个日期的差异,并将其从整体变量中删除。并回忆NSTimer
。
[Countdown invalidate];
Countdown = nil;
overallBackground = [saveOverall integerForKey:overall_save];
int newOverall = (int)overallBackground - secondsInBackground;
overall = newOverall;
[self setTimer];
When i set it after re-entering the app from background
我遇到的问题是,即使我已经进入后台并在重新启动Timer时重置整个变量,之前的整体变量数仍然存在,但变量也存储了新数字!
2014-05-11 16:48:18.759 Timer[836:60b] overall:3595 - Old number
2014-05-11 16:48:19.044 Timer[836:60b] overall:3591 - New number
2014-05-11 16:48:19.759 Timer[836:60b] overall:3594 - Old number
2014-05-11 16:48:20.044 Timer[836:60b] overall:3590 - New number
NSLog(@"overall:%d", overall);
overall = overall - 1;
[saveOverall setInteger:overall forKey:overall_save];
[saveOverall synchronize];
hours = overall / 3600;
minutes = overall - (hours * 3600);
minCon = minutes / 60;
seconds = minutes - (minCon * 60);
从上面的日志中你可以看到我的意思。我正在将完全相同的变量打印到日志中,只是你知道。
我的问题是为什么会发生这种情况?我怎么阻止它?