gpt4 book ai didi

ios - NSTimer 和 NSDateFormatter 麻烦。我的 timerLabel 刷新后消失了吗?

转载 作者:行者123 更新时间:2023-11-29 03:39:01 25 4
gpt4 key购买 nike

我在一个 viewController 上有一个 UIDatePicker,它允许用户以(7 月 22 日星期日下午 2:33)的形式设置日历日期、小时和分钟,以便在单独的 viewController 中使用计时器。在单独的 viewController 上,我尝试将计时器以“dd:hh:mm:ss”的形式显示在 UIImagePickerController 上的叠加层中,位于当前 NSDate(今天的日期/时间)和所选 datePicker 日期之间。基本上,它是一个倒计时器,当相机加载时,从选定的选择器日期开始倒计时天、小时、分钟、秒。我的问题是,一旦相机加载,计时器就会以正确的格式显示正确的剩余时间,但当它到达我的refreshLabel方法时很快就会消失。我记录了timerLabel的值,它在日志中正确显示,但是一旦循环运行,它就会为所有内容返回Null。昨晚我得到了一些帮助,确定我的问题很可能与字符串中日期的格式有关,但我不知道这可能是什么错误,因为相机首次加载时最初显示了正确的日期。这是我的第一个 viewController 中的代码,其中包含设置 timerLabels 时间的 datePicker:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showDate"]) {
NSString *textFieldContents = self.textField.text;
NSString *counterContents = self.counter;
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *deadline = [NSString stringWithFormat:@"%@/deadline.txt",
documentsDirectory];
NSString *flashName = [NSString stringWithFormat:@"%@/flashName.txt",
documentsDirectory];
NSError *fileError;
[textFieldContents writeToFile:flashName
atomically:YES
encoding:NSStringEncodingConversionAllowLossy
error:&fileError];
[counterContents writeToFile:deadline
atomically:YES
encoding:NSStringEncodingConversionAllowLossy
error:&fileError];
if(fileError.code == 0){
NSLog(@"deadline.txt was written successfully with these contents: %@,",
counterContents);
NSLog(@"flashName.txt was written successfully with these contents: %@,",
textFieldContents);}
else
[self performSegueWithIdentifier:@"showDate" sender:self];}
}

-(IBAction)refreshLabel; {

NSDate *selected = [NSDate dateWithTimeIntervalSince1970:[picker.date timeIntervalSince1970] - 1];
self.counter = [self.formatter stringFromDate:selected];
NSLog(@"selected is: %@", selected);
NSDate *todaysDate = [NSDate date];

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSUInteger unitFlags = NSDayCalendarUnit | NSMinuteCalendarUnit | NSHourCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *dateComparisonComponents = [gregorian components:unitFlags fromDate:todaysDate toDate:selected options:NSWrapCalendarComponents];
NSInteger days = [dateComparisonComponents day];
NSInteger hours = [dateComparisonComponents hour];
NSInteger minutes = [dateComparisonComponents minute];
NSInteger seconds = [dateComparisonComponents second];

self.counter = [NSString stringWithFormat:@"%ld:%02ld:%02ld:%02ld", (long)days, (long)hours, (long)minutes, (long)seconds];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *deadline = [NSString stringWithFormat:@"%@/deadline.txt", documentsDirectory];
NSInteger success = [NSKeyedArchiver archiveRootObject:selected toFile:deadline];
NSLog(@"success is: %d",success);
}e *selected = [NSDate dateWithTimeIntervalSince1970:[[picker date] timeIntervalSince1970] - 1];

- (void)viewDidLoad
{ [super viewDidLoad];
self.textField.delegate = self;

self.formatter = [NSDateFormatter new];
[self.formatter setDateFormat:@"dd:hh:mm:ss"];

NSDate *now = [NSDate date];

picker.minimumDate = [NSDate date];
picker.maximumDate = [now dateByAddingTimeInterval:604800];

[picker setDate:now animated:YES];
self.counter = [now description];

self.now = [NSDate date];
self.counter = [self.formatter stringFromDate:self.now];}

这是我的 UIImagePickerController 的 viewController 代码,我需要它来显示计时器标签并正确倒计时:

  - (IBAction)startCamera:(id)sender {
if (self.image == nil && [self.videoFilePath length] == 0) {
self.imagePickerController = [[UIImagePickerController alloc] init];
self.imagePickerController.delegate = self;
self.imagePickerController.allowsEditing = NO;
self.imagePickerController.videoMaximumDuration = 10;

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
}
else {
self.imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
self.imagePickerController.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:self.imagePickerController.sourceType];
[self presentViewController:self.imagePickerController animated:NO completion:nil];}

[[NSBundle mainBundle] loadNibNamed:@"OverlayView" owner:self options:nil];
self.overlayView.frame = CGRectMake(160,8,0,0);
self.imagePickerController.cameraOverlayView = self.overlayView;

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *deadline = [NSString stringWithFormat:@"%@/deadline.txt",
documentsDirectory];
NSString *flashName = [NSString stringWithFormat:@"%@/flashName.txt",
documentsDirectory];
NSError *fileError;
titleLabel.text = [NSString stringWithContentsOfFile:flashName
encoding:NSASCIIStringEncoding
error:&fileError];
timerLabel.text = [NSString stringWithContentsOfFile:deadline
encoding:NSASCIIStringEncoding
error:&fileError];
if(fileError.code == 0){
NSLog(@"deadline.txt was read successfully with these contents: %@,",
timerLabel.text);
NSLog(@"flashName.txt was read successfully with these contents: %@,",
titleLabel.text);}

self.startDate = [NSKeyedUnarchiver unarchiveObjectWithFile:deadline];
[self refreshLabel];

[NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(refreshLabel)
userInfo:nil
repeats:YES];
}

-(void)refreshLabel {

NSDate *todaysDate = [NSDate date];

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSUInteger unitFlags = NSDayCalendarUnit | NSMinuteCalendarUnit | NSHourCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *dateComparisonComponents = [gregorian components:unitFlags fromDate:todaysDate toDate:self.startDate options:NSWrapCalendarComponents];
NSInteger days = [dateComparisonComponents day];
NSInteger hours = [dateComparisonComponents hour];
NSInteger minutes = [dateComparisonComponents minute];
NSInteger seconds = [dateComparisonComponents second];

self.timerLabel.text = [NSString stringWithFormat:@"%ld:%02ld:%02ld:%02ld", (long)days, (long)hours, (long)minutes, (long)seconds];
if (days == 0 && hours == 0 && minutes == 0 && seconds == 0) {
[self.timer invalidate];
NSLog(@"Time's up!");
}
}

最佳答案

根据我们昨晚的讨论,您不能以这种方式使用日期格式化程序,因为您查看的是 future 的时间,例如 1 天 2 小时 5 分钟,而不是日期。一种方法是将选取器的选定日期传递给图像选取器 Controller ,并执行与在第一个 Controller 中所做的几乎相同的操作来获取日期组件。所以在第一个 Controller 中,将选择器日期保存到文件而不是字符串:

-(IBAction)refreshLabel; {

NSDate *selected = [NSDate dateWithTimeIntervalSince1970:[picker.date timeIntervalSince1970] - 1];
self.counter = [self.formatter stringFromDate:selected];
NSLog(@"selected is: %@", selected);
NSDate *todaysDate = [NSDate date];

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSUInteger unitFlags = NSDayCalendarUnit | NSMinuteCalendarUnit | NSHourCalendarUnit | NSSecondCalendarUnit;
dateComparisonComponents = [gregorian components:unitFlags fromDate:todaysDate toDate:selected options:NSWrapCalendarComponents];
NSInteger days = [dateComparisonComponents day];
NSInteger hours = [dateComparisonComponents hour];
NSInteger minutes = [dateComparisonComponents minute];
NSInteger seconds = [dateComparisonComponents second];

self.counter = [NSString stringWithFormat:@"%ld:%02ld:%02ld:%02ld", (long)days, (long)hours, (long)minutes, (long)seconds];
self.textField.text = self.counter;
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *deadline = [NSString stringWithFormat:@"%@/deadline.txt", documentsDirectory];
NSInteger success = [NSKeyedArchiver archiveRootObject:selected toFile:deadline];
NSLog(@"success is: %d",success);
}

然后在图像选择器 Controller 中(我只显示计时器标签所需的代码):

- (IBAction)startCamera:(id)sender {

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *deadline = [NSString stringWithFormat:@"%@/deadline.txt", documentsDirectory];
NSError *fileError;
self.startDate = [NSKeyedUnarchiver unarchiveObjectWithFile:deadline];
[self refreshLabel];
if(fileError.code == 0){
NSLog(@"deadline.txt was read successfully with these contents: %@,", self.timerLabel.text);
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(refreshLabel) userInfo:nil repeats:YES];
}
}


-(void)refreshLabel{
NSDate *todaysDate = [NSDate date];

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSUInteger unitFlags = NSDayCalendarUnit | NSMinuteCalendarUnit | NSHourCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *dateComparisonComponents = [gregorian components:unitFlags fromDate:todaysDate toDate:self.startDate options:NSWrapCalendarComponents];
NSInteger days = [dateComparisonComponents day];
NSInteger hours = [dateComparisonComponents hour];
NSInteger minutes = [dateComparisonComponents minute];
NSInteger seconds = [dateComparisonComponents second];

self.timerLabel.text = [NSString stringWithFormat:@"%ld:%02ld:%02ld:%02ld", (long)days, (long)hours, (long)minutes, (long)seconds];
if (days == 0 && hours == 0 && minutes == 0 && seconds == 0) {
[self.timer invalidate];
NSLog(@"Time's up!");
}
}

关于ios - NSTimer 和 NSDateFormatter 麻烦。我的 timerLabel 刷新后消失了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18729607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com