Xcode 在UIDatePicker中禁用特定日期
我正试图设置一个uidatepicker来预订日历中的一些活动。 我需要在UIDatePicker中禁用(隐藏)某些特定日期,例如我出城或度假的日子 你能帮我做这个吗? Thanx提前了很多时间。 下面是一些代码:Xcode 在UIDatePicker中禁用特定日期,xcode,uidatepicker,Xcode,Uidatepicker,我正试图设置一个uidatepicker来预订日历中的一些活动。 我需要在UIDatePicker中禁用(隐藏)某些特定日期,例如我出城或度假的日子 你能帮我做这个吗? Thanx提前了很多时间。 下面是一些代码: - (void)viewDidLoad { NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"IT"]; [self.datePicker setLocale:locale];
- (void)viewDidLoad
{
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"IT"];
[self.datePicker setLocale:locale];
NSString *min = @"20132015";
NSString *max = @"22132015";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"ddMMyyyy HH:mm"];
NSDate *datemin = [dateFormat dateFromString:min];
NSDate *datemax = [dateFormat dateFromString:max];
[self.view addSubview:datePicker];
datePicker.minuteInterval = 15;
datePicker.maximumDate = datemax;
datePicker.minimumDate = [NSDate date];
[datePicker addTarget:self action:@selector(disableDate) forControlEvents:UIControlEventValueChanged];
[datePicker addTarget:self
action:@selector(LabelChange:)
forControlEvents:UIControlEventValueChanged];
[super viewDidLoad];
// Do any additional setup after loading the view.
}
-(void)disableDate{
NSDate *pickedDate = datePicker.date; // Get current Date
NSDate *disabledDate = [?????;
if([pickedDate compare:disabledDate] == NSOrderedSame)
{
[datePicker setDate:disabledDate animated:YES];
}
}
-(void)getDisableDate
{
?????
}
- (void)LabelChange:(id)sender{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd.MM.yyyy HH:mm"];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"IT"];
[self.datePicker setLocale:locale];
labelDate.text = [NSString stringWithFormat:@"%@",
[df stringFromDate:datePicker.date]];
NSLog(@"date:%@", datePicker.date);
}
UIDatePicker不支持日期范围。相反,我要做的是,只要选择了错误的日期,就使用
setDate:animated:
将日期更改为可选择的最近日期。另外,在某处显示一条消息,解释所选日期更改的原因
或者,使用UIPickerView编写您自己的自定义UIDatePicker。不太难,我以前做过。最糟糕的部分是处理包装值(例如:31->1)。不过,这需要更多的工作。最好的解决方案是使用UIPickerView创建自定义UIDatePicker。这太简单了,将来您可以轻松地进行更改 检查一下