0

我正在尝试在表单中的标签中显示与UIDatePicker输入时间和真实世界时间的时差。我显示了正确的信息,但我很难让相同的数据显示在我的标签中。截至目前,我的 UILabel 中没有显示任何内容,但正确的数据在 NSLog 中。当我改变IBOutletdd:hh:mm:ssNSLog

[self.formatter stringFromDate:timeDif];

[self.formatter stringFromDate:selected];

显示当前 DatePicker 选择时间(这不是我想要的),但 NSLog 仍然正确显示时差。我知道我的格式化顺序有问题,但我无法看到哪里出错了。

- (IBAction)displayDate:(id)sender {

    NSDate *timeDif = [self.formatter dateFromString:self.string];
    NSDate *todaysDate = [NSDate date];
    NSDate *selected = [picker date];

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

    [picker setDate:selected animated:YES];

    self.label.text = [self.formatter stringFromDate:timeDif];

    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];

    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];

    dateComponents.day = days;
    dateComponents.hour = hours;
    dateComponents.minute = minutes;
    dateComponents.second = seconds;

    self.string = [NSString stringWithFormat:@"%1d:%1d:%1d:%1d",
                                              dateComparisonComponents.day,
                                              dateComparisonComponents.hour,
                                              dateComparisonComponents.minute,
                                              dateComparisonComponents.second];

     NSLog(@"%1d:%1d:%1d:%1d",
                             dateComparisonComponents.day,
                             dateComparisonComponents.hour,
                             dateComparisonComponents.minute,
                             dateComparisonComponents.second);
}
4

1 回答 1

1

你有很多额外的和不必要的代码。只需这样做:

- (IBAction)displayDate:(id)sender {
    NSDate *todaysDate = [NSDate date];
    NSDate *selected = [picker 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.label.text = [NSString stringWithFormat:@"%ld:%02ld:%02ld:%02ld",
                                              days,
                                              hours,
                                              minutes,
                                              seconds];
}

在您的原始代码中,self.formatter甚至不会在第一次运行代码时设置。这意味着这timeDif将是nil第一次。您还可以获取选择器的日期,然后将选择器的日期设置为相同的值。没有理由。而且您dateComponents无缘无故地使用。最后,是self.string为了什么?

于 2013-09-01T06:39:01.187 回答