0

当用户单击 a时,UITextFieldUIDatePicker将使用下面的代码显示输入。但是当我这样做时,日期选择器和普通文本键盘会同时显示。我将如何禁用键盘显示?

- (IBAction)selectDateBegin:(id)sender {

if ([self.view viewWithTag:9]) {
    return;
}
CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height-216-44, 320, 44);
CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height-216, 320, 216);

UIView *darkView = [[UIView alloc] initWithFrame:self.view.bounds];
darkView.alpha = 0;
darkView.backgroundColor = [UIColor blackColor];
darkView.tag = 9;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissDatePicker:)] ;
[darkView addGestureRecognizer:tapGesture];
[self.view addSubview:darkView];

UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height+44, 320, 216)] ;
datePicker.tag = 10;
[datePicker addTarget:self action:@selector(changeDate:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];

UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)];
toolBar.tag = 11;
toolBar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissDatePicker:)];
[toolBar setItems:[NSArray arrayWithObjects:spacer, doneButton, nil]];
[self.view addSubview:toolBar];

[UIView beginAnimations:@"MoveIn" context:nil];
toolBar.frame = toolbarTargetFrame;
datePicker.frame = datePickerTargetFrame;
darkView.alpha = 0.5;
[UIView commitAnimations];}
4

2 回答 2

1
-(void)textFieldDidBeginEditing:(UITextField *)sender
{
     [self.textFieldName resignFirstResponder];
   //
   //  Code ...continue,......for display DatePicker
   //
}
于 2013-03-09T10:59:58.623 回答
0

为您的日期选择器(导航栏具有确定和取消按钮)容器创建一个单独的 UIView,然后将其分配给textfield.inputView属性。有关更多详细信息,请参阅UITextField 类参考

这将加载您的日期选择器视图而不是默认键盘。

您会在网上找到更多相同的示例。

希望这是您正在寻找的。

于 2013-03-09T11:01:57.197 回答