【发布时间】:2014-09-02 02:57:05
【问题描述】:
我有一个从当前日期到 10 分钟过去的倒计时。
当我在视图中倒计时完全正常,但是当我改变视图并返回时,计时器停止,有人能指出我正确的方向吗?
-(void)updateCountdown {
dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"YYYY-MM-dd-HH-mm-ss"];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSUInteger unitFlags = NSMinuteCalendarUnit|NSSecondCalendarUnit;
NSDateComponents *dateComponants = [calendar components:unitFlags fromDate:startingDate toDate:endingDate options:0];
NSInteger minutes = [dateComponants minute];
NSInteger seconds = [dateComponants second];
NSString *countdownText = [NSString stringWithFormat:@"Free Check: %ld:%ld", (long)minutes, (long)seconds];
timeLabel.text = countdownText;
NSLog (@"Startdate is %@", startingDate);
NSLog(@"Enddate is %@", endingDate);
//Attempt at saving the time but I guess this only saves the text?
NSString * saveStringTime = timeLabel.text;
NSUserDefaults * defaultsTime = [NSUserDefaults standardUserDefaults];
[defaultsTime setObject:saveStringTime forKey:@"saveStringTime"];
[defaultsTime synchronize];
[self performSelector:@selector(updateCountdown) withObject:nil afterDelay:1];
}
我认为我的 ViewDidLoad 中需要一些东西来获取它的任何时间并将其放回?
谢谢
【问题讨论】:
-
您的视图控制器在您离开时是否被释放并在您回来时重新分配?
-
我不确定,如何检查?
-
您可以通过实现 dealloc 并将日志放入其中进行检查,和/或将日志放入 viewDidLoad 并查看当您返回控制器时是否再次调用它。
-
即使显示的代码没有使用
dateFormatter,请注意YYYY应该是yyyy。请参阅Date Formatting Guide 中的固定格式部分(查找“一个常见的错误是使用 YYYY...”)。另见unicode.org/reports/tr35/tr35-31/…。
标签: ios objective-c xcode nsdate