日期选择器 - 显示两个日期之间的时间(UIDatePickerModeDateAndTime)

时间:2014-07-22 00:00:45

标签: ios objective-c date datepicker

我目前正在使用Date Picker UIDatePickerModeCountDownTimer- (UIDatePicker *)datePicker { if (!_datePicker) { _datePicker = [[UIDatePicker alloc] init]; _datePicker.datePickerMode = UIDatePickerModeCountDownTimer; _datePicker.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.8]; } return _datePicker; } - (void)setDuration:(NSTimeInterval)duration { _duration = duration; self.datePicker.countDownDuration = _duration; } 使用:

- (void)update {
    if (self.time) {
        [self setImage:nil forState:UIControlStateNormal];

        NSString *title;
        NSTimeInterval timeInterval = [self.time doubleValue];

        if (self.ticking) {
            NSMutableString *dateFormat = [[NSMutableString alloc] init];

            if (timeInterval < 0) {
                [dateFormat appendString:@"-"];
            }

            if (fabsf(timeInterval) > 60 * 60) {
                [dateFormat appendString:@"hh:"];
            }

            [dateFormat appendString:@"mm:ss"];

            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            formatter.dateFormat = dateFormat;
            formatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];

            NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
            title = [formatter stringFromDate:date];

            if ([self.time integerValue] > 0) {
                self.circleView.backgroundColor = [UIColor colorWithRed:0.373 green:0.702 blue:0.522 alpha:1];
            } else {
                self.circleView.backgroundColor = [UIColor colorWithRed:0.820 green:0.373 blue:0.424 alpha:1];
            }
        } else {
            NSMutableString *text = [[NSMutableString alloc] init];
            if (fabsf(timeInterval) < 60) {
                // Show seconds
                [text appendFormat:@"%.0fs", timeInterval];
            } else if (fabsf(timeInterval) < 60 * 60) {
                // Show minutes
                [text appendFormat:@"%.0fm", floorf(timeInterval / 60)];
            } else {
                // Show hours
                [text appendFormat:@"%.0fh", floorf(timeInterval / 60 / 60)];
            }

            title = text;
            self.circleView.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.2];
        }

        [self setTitle:title forState:UIControlStateNormal];

        return;
    }

    [self setTitle:nil forState:UIControlStateNormal];
    [self setImage:[UIImage imageNamed:@"plus"] forState:UIControlStateNormal];
    self.circleView.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.05];
}

...日期选择器,并显示(在标签中)从当前日期到将来选择的日期时间:

DatePicker

...但我已将UIDatePickerModeDateAndTime切换为update,并需要弄清楚如何使用它来更新我的{{1}}方法。

我需要在标签中显示月/日/秒之外的月/日。

1 个答案:

答案 0 :(得分:1)

如果你想要一个像委托这样的方法,那么这可以帮助你..

将目标添加到日期选择器....

[myDatePicker addTarget:self action:@selector(onPickerValueChanged:) forControlEvents:UIControlEventValueChanged];

删除dealloc中的目标。否则,如果您的选择器正在滚动并且弹出了viewController,则应用程序将崩溃。

- (void dealloc
{
    [myPicker removeTarget:self action:@selector(onPickerValueChanged:) forControlEvents:UIControlEventValueChanged];
    [myPicker release];//FOR NON ARC
    [super dealloc];//FOR NON ARC
}

实施价值变更,如

- (IBAction)onPickerValueChanged:(id)sender
{
    [self update];
}