0

我在一个 viewController 上有一个 UIDatePicker,它允许用户以(7 月 22 日星期日,下午 2:33)的形式设置日历日期、小时和分钟,以便在单独的 viewController 中使用计时器。在单独的 viewController 上,我试图在 UIImagePickerController 上以“dd:hh:mm:ss”的形式显示在当前 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 代码,我需要显示 timerLabel 并正确倒计时:

  - (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!");
    }
}
4

1 回答 1

1

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

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

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

- (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!");
    }
}
于 2013-09-11T00:04:21.037 回答