现在我已经设置了UIDatePicker的minimumDate和maximumDate,就像这个
一样_timePicker.minimumDate = [[NSDate date] dateByAddingTimeInterval:60];
_timePicker.maximumDate = [[NSDate date] dateByAddingTimeInterval:86400*10];
我添加了一个目标
[_timePicker addTarget:self action:@selector(labelContentDidChange:) forControlEvents:UIControlEventValueChanged];
我想在用户选择不在该范围内的日期时显示警告,但每次调用labelContentDidChange时,日期属性都会自动更改,我无法获得用户选择的内容之前,例如,如果我当前选择时间,则datePicker将滚动到一分钟后的时间,,而我在labelContentDidChange中的日期已经过了一分钟。
答案 0 :(得分:0)
也许将用户选择的日期与最短和最长日期进行比较?
//假设我们有NSDate * pickedDate
NSDate *minimumDate = [[NSDate date] dateByAddingTimeInterval:60];
NSDate *maximumDate = [[NSDate date] dateByAddingTimeInterval:86400*10];
// if picked date is earlier than minimumDate, OR if it is later than maximumDate - show alert
if ([pickedDate compare:minimumDate] == NSOrderedAscending ||
[pickedDate compare:maximumDate] == NSOrderedDescending) {
// show your alert
} else {
// valid date was picked
}
虽然如果你将这些最小值和最大值设置为你的datePicker用户将无法选择无效日期,但它只适用于其他一些自定义时段...