How to hide default keyboard

為{幸葍}努か 提交于 2019-12-04 13:50:38

问题


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.



来源:https://stackoverflow.com/questions/649777/how-to-hide-default-keyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!