我想知道你们中有些人是否可以就此提出建议。
我正在尝试将NSDatePicker的步长更改为另一个值而不是1。 最重要的是,我发现步进分钟不会改变时间,也不会改变一天。
我正在使用委托方法datePickerCell:validateProposedDateValue:timeInterval:。
现在,虽然它确实按预期工作,整个事情看起来如此之多,以至于我开始怀疑是否有更简单的方法来实现这一目标。
对文档的任何建议或指示表示赞赏。谢谢。
这是我的代码:
- (void)datePickerCell:(NSDatePickerCell *)aDatePickerCell validateProposedDateValue:(NSDate **)proposedDateValue
timeInterval:(NSTimeInterval *)proposedTimeInterval {
DLog(@"date picker for: %@", [aDatePickerCell identifier]);
NSDate *newProposedDateValue = nil;
// just in case that we don't need a correction
NSDate *correctedProposedDateValue = *proposedDateValue;
// the interval that the step generates
// > 0 means: the old date is later than the new proposed date
// < 0 means: the old date is earlier than the new proposed date
int interval = [[self dateValue] timeIntervalSinceDate:*proposedDateValue];
// define expected interval values for our scenarios
// we don't care about a minute step that does not cross the hour here
// nor do we care about an hour step that does not cross the day
// minutes are stepped: minute is stepped but hour remains (01:59 <-> 01:00), so the difference is 59 minutes
int const minuteSteppedUpAcrossHour = -59 *60;
int const minuteSteppedDownAcrossHour = - minuteSteppedUpAcrossHour;
// nor do we care about an hour step that does not cross the day
// hours are stepped: hour is stepped but day remains (10.03.13 00:30 <-> 10.03.13 23:30) we have a difference of 23 hours
int const hourSteppedUpAcrossDay = -23 *60 *60;
int const hourSteppedDownAcrossDay = - hourSteppedUpAcrossDay;
// define correction values for our scenarios
int const anHour = 60 *60;
int const aDay = anHour *24;
switch (interval) {
case hourSteppedUpAcrossDay:
correctedProposedDateValue = [*proposedDateValue dateByAddingTimeInterval:(-aDay)];
break;
case minuteSteppedDownAcrossHour:
correctedProposedDateValue = [*proposedDateValue dateByAddingTimeInterval:(+anHour)];
break;
case hourSteppedDownAcrossDay:
correctedProposedDateValue = [*proposedDateValue dateByAddingTimeInterval:(+aDay)];
break;
case minuteSteppedUpAcrossHour:
correctedProposedDateValue = [*proposedDateValue dateByAddingTimeInterval:(-anHour)];
break;
default:
break;
}
if ([self dateValue] < correctedProposedDateValue) {
newProposedDateValue = [self roundDateUpForMinuteIntervalConstraint:correctedProposedDateValue];
} else {
newProposedDateValue = [self roundDateDownForMinuteIntervalConstraint:correctedProposedDateValue];
}
*proposedDateValue = newProposedDateValue;
}
- (NSDate *)roundDateUpForMinuteIntervalConstraint:(NSDate *)date {
return [self date:date roundedUpToMinutes:MINUTE_INTERVAL_CONSTRAINT_FOR_SESSIONS_START];
}
- (NSDate *)roundDateDownForMinuteIntervalConstraint:(NSDate *)date {
return [self date:date roundedDownToMinutes:MINUTE_INTERVAL_CONSTRAINT_FOR_SESSIONS_START];
}
- (NSDate *)date:(NSDate *)date roundedUpToMinutes:(int)minutes {
// Strip miliseconds by converting to int
int referenceTimeInterval = (int)[date timeIntervalSinceReferenceDate];
int remainingSeconds = referenceTimeInterval %(60 *minutes);
int timeRoundedUpToMinutes = 0;
if (remainingSeconds== 0) {
timeRoundedUpToMinutes = referenceTimeInterval;
} else {
timeRoundedUpToMinutes = referenceTimeInterval -remainingSeconds +(60 *minutes);
}
return [NSDate dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)timeRoundedUpToMinutes];
}
- (NSDate *)date:(NSDate *)date roundedDownToMinutes:(int)minutes {
// Strip miliseconds by converting to int
int referenceTimeInterval = (int)[date timeIntervalSinceReferenceDate];
int remainingSeconds = referenceTimeInterval %(60 *minutes);
int timeRoundedUpToMinutes = referenceTimeInterval -remainingSeconds;
return [NSDate dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)timeRoundedUpToMinutes];
}