我在UITableView中有一个UITextField。我试图将UIPickerView或UIDatePicker作为其输入视图。这两个选项都出现了,在UIPickerView的情况下,它正在加载正确的数据。问题是它不可滚动,用户无法与之交互,选择并在各种选项之间滚动。
Userinteraction设置为yes,委托是正确的,一切都已就绪。
这是代码:
- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
UIDatePicker *datepicker = [[UIDatePicker alloc] initWithFrame:CGRectZero];
[datepicker setDatePickerMode:UIDatePickerModeDate];
textField.inputView = datepicker;
return YES;
}
答案 0 :(得分:1)
为什么要在此方法中设置inputView
?
在viewDidLoad
中执行此操作。这是一个配置,
每次用户关注inputView
:
textField
都没有意义
- (void)viewDidLoad {
[super viewDidLoad];
UIDatePicker *datePicker = [[UIDatePicker alloc]init];
[datePicker setDate:[NSDate date]];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:@selector(editTextField:) forControlEvents:UIControlEventValueChanged];
[textField setInputView:datePicker];
}
然后:
- (void)editTextField:(id)sender
{
UIDatePicker *picker = (UIDatePicker *)textField.inputView;
[picker setMaximumDate:[NSDate date]];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSDate *eventDate = picker.date;
[dateFormat setDateFormat:@"dd/MM/yyyy"];
NSString *dateString = [dateFormat stringFromDate:eventDate];
textField.text = [NSString stringWithFormat:@"%@",dateString];
}