我有一个标签,上面显示了倒数计时器.
I have a label on which I am showing countdown timer.
现在,如果我关闭我的应用,计时器将关闭,标签文本也将关闭.我知道我们可以保存标签的文本值.但是,当应用再次启动时,我们如何显示正确的倒计时.
Now if I close my app the timer will be off and the label's text also. I know that we can save the label's text value. But how do we show the correct countdown when the app starts again.
假设我再次启动应用3分钟后,我在00:05:35关闭,标签应该显示00:02:35,并且计时器应该在那里剩余倒计时
Suppose I close at 00:05:35 after 3 minutes when app is launched again the label should show 00:02:35 and the timer should be there for remaining countdown
推荐答案是的,只需在NSUserDefaults中存储关闭应用程序的时间和倒计时所需的时间.当应用再次启动时,您会从NSUserDefaults获得关闭时间以及剩余时间.使用当前时间,这是简单的数学运算,可以计算倒计时剩余的校正时间.
Yes, simply store the time at which your app was closed and the time left to count down in NSUserDefaults. When the app starts again you get the time it was closed from NSUserDefaults and the time left. Using the current time it's simple math calculating the corrected time left on your count down.
像这样的事情可能会成功完成,当然未经测试:
Something like this might do the trick, untested of course:
// save state NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; NSDate *now = [NSDate date]; double countDown = 45.0; // in seconds, get this from your counter [userDefaults setObject:now forKey:@"timeAtQuit"]; [userDefaults setDouble:countDown forKey:@"countDown"]; [userDefaults synchronize]; // restore state NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; NSDate *timeAtQuit = [userDefaults objectForKey:@"timeAtQuit"]; double timeSinceQuit = [timeAtQuit timeIntervalSinceNow]; double countDown = timeSinceQuit + [userDefaults doubleForKey:@"countDown"];更多推荐
是否可以在“用户默认设置"中保存计时器的状态?
发布评论