当我在文本字段中“触摸”时,我用它来弹出日期选择器视图。也就是说,我需要文本字段来显示我在日期选择器视图中选择的内容作为其内容。
- (IBAction)showPicker:(id)sender {
// create a UIPicker view as a custom keyboard view
UIDatePicker* pickerView = [[UIDatePicker alloc] init];
[pickerView sizeToFit];
pickerView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
//pickerView.delegate = self;
//pickerView.dataSource = self;
//pickerView.showsSelectionIndicator = YES;
self.datePickerView = pickerView; //UIDatePicker
fromDate.inputView = pickerView;
// create a done view + done button, attach to it a doneClicked action, and place it in a toolbar as an accessory input view...
// Prepare done button
UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
keyboardDoneButtonView.barStyle = UIBarStyleBlack;
keyboardDoneButtonView.translucent = YES;
keyboardDoneButtonView.tintColor = nil;
[keyboardDoneButtonView sizeToFit];
UIBarButtonItem* doneButton = [[[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStyleBordered target:self
action:@selector(pickerDoneClicked:)] autorelease];
fromDate.text = (NSString *)datePickerView.date; // fromDate is the Text Field outlet, where I am trying to display the selection on the picker.
[keyboardDoneButtonView setItems:[NSArray arrayWithObjects:doneButton, nil]];
// Plug the keyboardDoneButtonView into the text field...
fromDate.inputAccessoryView = keyboardDoneButtonView;
[pickerView release];
[keyboardDoneButtonView release];
}
- (IBAction)pickerDoneClicked:(id)sender {
[fromDate resignFirstResponder];
}
编辑:我可以在屏幕上为一个文本字段执行此操作。无论如何,只有键盘弹出第二个文本字段。
为第二个文本字段执行此操作的任何想法?
答案 0 :(得分:0)
fromDate.inputView = pickerView;
这行代码使选择器视图为fromDate字段的输入视图。您需要其他日期字段的类似代码。您还需要在其他字段上设置输入附件。
创建一个特殊方法来执行此操作并触发所有文本字段上的操作是不必要的工作。只需在viewDidLoad
中创建选择器视图和工具栏,并将它们分配给您希望它们所在的任何字段的输入视图和附件视图。已读取的文本字段具有触发方法,该方法将调出键盘。
答案 1 :(得分:0)
只是您问题的替代解决方案。
1)在您想要显示UIButton
UIDatePickerView
2)将textfield的委托设置为nil,以显示选择器视图。
fromDate.delegate = nil;
3)在该自定义按钮的IBAction
(“触地”)上写下以下代码: -
- (IBAction) customButtonAction: (id) sender
{
[_previousTextField resignFirstResponder]; // The instance of last textfield tapped.
[self showDatePicker];
}
4)点击Picker视图的“完成”按钮,将所选值设置为文本字段
- (IBAction)pickerDoneClicked:(id)sender
{
fromDate.text = @"value from UIPickerView's selected row"
}