根据类型限制 UIDatePicker 控件,比如(成人、儿童、婴儿)Objective C
Restrict the UIDatePicker control based on type, say (Adult, Child, Infant) Objective C
我正在尝试根据类型限制 UIDatePicker 控件,例如(成人、儿童、婴儿)。下面是目前正在做的调用 DatePicker
cell.leftDetailTextField.inputView=[self customDatePickerView];
-(UIDatePicker *)customDatePickerView{
NSLog (@"Date picker called");
datePicker = [[UIDatePicker alloc]init];
[datePicker setDatePickerMode:UIDatePickerModeDate];
//datePicker.maximumDate=[NSDate date]; // to prevent future date selection:
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:-90];
NSDate *minDate = [gregorian dateByAddingComponents:comps toDate:currentDate options:0];
[comps setYear:-12];
NSDate *maxDate = [gregorian dateByAddingComponents:comps toDate:currentDate options:0];
datePicker.minimumDate = minDate;
datePicker.maximumDate = maxDate;
[datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
return datePicker;
}
如何将值动态传递给此方法调用。我需要通过 Adult(val 1), Children(Val 2), Infant (val 3)
-(UIDatePicker *)customDatePickerView:(NSInteger)val{
datePicker = [[UIDatePicker alloc]init];
[datePicker setDatePickerMode:UIDatePickerModeDate];
//datePicker.maximumDate=[NSDate date]; // to prevent future date selection:
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init];
NSDate *minDate, *maxDate;
switch (val) {
case 1: // Adult
[comps setYear:-90];
minDate = [gregorian dateByAddingComponents:comps toDate:currentDate options:0];
[comps setYear:-12];
maxDate = [gregorian dateByAddingComponents:comps toDate:currentDate options:0];
break;
case 2: // Child
[comps setYear:-12];
minDate = [gregorian dateByAddingComponents:comps toDate:currentDate options:0];
[comps setYear:-2];
maxDate = [gregorian dateByAddingComponents:comps toDate:currentDate options:0];
break;
case 3: // Infant
[comps setYear:-2];
minDate = [gregorian dateByAddingComponents:comps toDate:currentDate options:0];
[comps setYear:-0];
maxDate = [gregorian dateByAddingComponents:comps toDate:currentDate options:0];
break;
default:
break;
}
datePicker.minimumDate = minDate;
datePicker.maximumDate = maxDate;
[datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
return datePicker;
}
- (void) dateChanged:(UIDatePicker *)sender{
UIDatePicker *datePicker=(UIDatePicker *)sender;
NSDateFormatter *dateFormat =[[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"dd-MMM-yyyy"];
self.activeTextField.text = [dateFormat stringFromDate:datePicker.date];
}
我正在尝试根据类型限制 UIDatePicker 控件,例如(成人、儿童、婴儿)。下面是目前正在做的调用 DatePicker
cell.leftDetailTextField.inputView=[self customDatePickerView];
-(UIDatePicker *)customDatePickerView{
NSLog (@"Date picker called");
datePicker = [[UIDatePicker alloc]init];
[datePicker setDatePickerMode:UIDatePickerModeDate];
//datePicker.maximumDate=[NSDate date]; // to prevent future date selection:
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:-90];
NSDate *minDate = [gregorian dateByAddingComponents:comps toDate:currentDate options:0];
[comps setYear:-12];
NSDate *maxDate = [gregorian dateByAddingComponents:comps toDate:currentDate options:0];
datePicker.minimumDate = minDate;
datePicker.maximumDate = maxDate;
[datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
return datePicker;
}
如何将值动态传递给此方法调用。我需要通过 Adult(val 1), Children(Val 2), Infant (val 3)
-(UIDatePicker *)customDatePickerView:(NSInteger)val{
datePicker = [[UIDatePicker alloc]init];
[datePicker setDatePickerMode:UIDatePickerModeDate];
//datePicker.maximumDate=[NSDate date]; // to prevent future date selection:
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init];
NSDate *minDate, *maxDate;
switch (val) {
case 1: // Adult
[comps setYear:-90];
minDate = [gregorian dateByAddingComponents:comps toDate:currentDate options:0];
[comps setYear:-12];
maxDate = [gregorian dateByAddingComponents:comps toDate:currentDate options:0];
break;
case 2: // Child
[comps setYear:-12];
minDate = [gregorian dateByAddingComponents:comps toDate:currentDate options:0];
[comps setYear:-2];
maxDate = [gregorian dateByAddingComponents:comps toDate:currentDate options:0];
break;
case 3: // Infant
[comps setYear:-2];
minDate = [gregorian dateByAddingComponents:comps toDate:currentDate options:0];
[comps setYear:-0];
maxDate = [gregorian dateByAddingComponents:comps toDate:currentDate options:0];
break;
default:
break;
}
datePicker.minimumDate = minDate;
datePicker.maximumDate = maxDate;
[datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
return datePicker;
}
- (void) dateChanged:(UIDatePicker *)sender{
UIDatePicker *datePicker=(UIDatePicker *)sender;
NSDateFormatter *dateFormat =[[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"dd-MMM-yyyy"];
self.activeTextField.text = [dateFormat stringFromDate:datePicker.date];
}