禁用键盘时

时间:2013-03-09 10:55:34

标签: ios objective-c ios6 uitextfield uistoryboard

当用户点击UITextField时,我使用下面的代码显示输入UIDatePicker。但是当我这样做时,datepicker和普通文本键盘同时显示。如何禁用键盘显示?

- (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];}

2 个答案:

答案 0 :(得分:1)

-(void)textFieldDidBeginEditing:(UITextField *)sender
{
     [self.textFieldName resignFirstResponder];
   //
   //  Code ...continue,......for display DatePicker
   //
}

答案 1 :(得分:0)

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

这将加载你的datepicker视图而不是默认键盘。

你会在网上找到更多相同的例子。

希望这就是你要找的东西。