stringWithFormat,dateFromString,& stringFromDate不起作用

时间:2013-09-01 06:28:30

标签: ios cocoa-touch nsdateformatter

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

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

1 个答案:

答案 0 :(得分: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是什么?