How to hide default keyboard

2019-03-22 07:37发布

问题:

I have 5 UITextFields added on a form. One of the textfileds is for entering the date of birth. So when that particular textfield is touched instead of displaying the default keyboard, datepicker needs to be displayed.

I tried the following:

- (void)textFieldDidBeginEditing:(UITextField *)textField{
   [textField resignFirstResponder];
   [self showDatePicker];//this is my own function to display datepicker.
}

when this function is called the datepicker is displayed, along with the date picker the default keyboard is also displayed and that hides the datepicker. I don't know how to get rid of the default keyboard, for this particular textfiled. Please help me.

回答1:

Though this is an old question, I came here while I was searching for the same.

This will work for any number/nesting of views presented in the screen,

- (void) hideKeyboard: (UIView *) view {
    NSArray *subviews = [view subviews];
    for (UIView *subview in subviews){
        if ([subview isKindOfClass:[UITextField class]]) {
            [subview resignFirstResponder];
        }
        [self hideKeyboard: subview ];
    }
}

Later I found more intuitive way to do it,

[self.view endEditing:YES];


回答2:

You can handle this in -textFieldShouldBeginEditing:.

If this method returns NO, the keyboard will not be displayed.

Simply show the date picker in this method and then return NO.



回答3:

Maybe you should not use a UITextField for this. Use a normal UITableViewCell and push a new UIViewController with the datepicker when tapped.



回答4:

I've just been working through this same problem,

We tried a few things and ended up with a lot of really horrible code and bugs along the way.

The simplest option by far is to still have a UITextField, but to set it's .inputView to the DatePicker

Now we are trying to find a nice way to submit the field, since the picker has no return button like the keyboards.



回答5:

Resigning the first responder ought to hide the keyboard. My best guess is that textField isn't actually the first responder at this point in the code, so telling it to resign has no effect. Perhaps you should insert an NSLog statement to check that.