如何隐藏默认键盘

发布于 2024-07-15 07:23:48 字数 389 浏览 8 评论 0原文

我在表单上添加了 5 个 UITextField。 其中一个文本字段用于输入出生日期。 因此,当触摸该特定文本字段而不是显示默认键盘时,需要显示日期选择器。

我尝试了以下操作:

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

调用此函数时,会显示日期选择器,以及日期选择器,还会显示默认键盘并隐藏日期选择器。 我不知道如何摆脱这个特定文本字段的默认键盘。 请帮我。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一向肩并 2024-07-22 07:23:48

虽然这是一个老问题,但我在寻找同样的问题时来到这里。

这适用于屏幕中呈现的任何数量/嵌套的视图,

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

后来我找到了更直观的方法,

[self.view endEditing:YES];

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];
冬天旳寂寞 2024-07-22 07:23:48

您可以在 -textFieldShouldBeginEditing: 中处理此问题。

如果该方法返回NO,则不会显示键盘。

只需在此方法中显示日期选择器,然后返回 NO。

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.

情场扛把子 2024-07-22 07:23:48

也许您不应该为此使用 UITextField。 使用普通的 UITableViewCell 并在点击时使用日期选择器推送新的 UIViewController。

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

守不住的情 2024-07-22 07:23:48

我刚刚解决了同样的问题,

我们尝试了一些方法,最终得到了很多非常可怕的代码和错误。

到目前为止,最简单的选择是仍然有一个 UITextField,但将其 .inputView 设置为 DatePicker

现在我们正在尝试找到一个不错的选择提交字段的方式,因为选择器没有像键盘那样的返回按钮。

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.

稀香 2024-07-22 07:23:48

辞职第一响应者应该隐藏键盘。 我最好的猜测是,此时 textField 实际上并不是代码中的第一响应者,因此告诉它退出没有任何效果。 也许您应该插入一个 NSLog 语句来检查这一点。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文