如何在NSDatePicker中重置所选日期(objective-c / cocoa)

时间:2011-03-14 19:13:37

标签: objective-c cocoa nsdatepicker

我在图形模式下有一个NSDatePicker,我设置了这个代码,每秒调用一次,以更新NSDatePicker对象(datePicker)中的当前时间。

- (void) updateTimerNoOne:(NSTimer *) timer {
    [datePicker setDateValue: [NSDate date]];
    [datePicker setNeedsDisplay: YES];
}

enter image description here - >调用updateTimerNoOne时重置 Selected date is reset when the method is called

问题在于,当我选择某个日期时,此功能始终会重置,以便所选日期消失。

如何保留所选日期?

1 个答案:

答案 0 :(得分:0)

应该从

修改每秒调用的方法
[datePicker setDateValue: [NSDate date]];

[datePicker setDateValue: [datePicker dateValue]];

将所选日期保持为突出显示。 Make a new NSDate by combining two NSDates

问题是dateValue的小时/分钟/秒没有改变,所以我需要这段代码来更新它们。

unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
NSDateComponents *comps = [[NSCalendar currentCalendar] components:unitFlags fromDate:[datePicker dateValue]];
NSDate *day3 = [[NSCalendar currentCalendar] dateFromComponents:comps];
unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
comps = [[NSCalendar currentCalendar] components:unitFlags fromDate:[NSDate date]];
day3 = [[NSCalendar currentCalendar] dateByAddingComponents:comps toDate:day3 options:0];