在 UITextField 中按下清除按钮时隐藏键盘

发布于 2024-10-15 03:45:38 字数 56 浏览 1 评论 0 原文

当按下 UITextField 的清除按钮时,有什么方法可以隐藏键盘吗?

Is there any way to hide the keyboard when a clear button of UITextField is pressed?

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

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

发布评论

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

评论(6

锦上情书 2024-10-22 03:45:38

是的,有,尽管我怀疑这样做会违反苹果人机界面指南。

为此,请将以下方法添加到视图控制器的实现文件中。然后将视图控制器放入文本字​​段的委托中。

- (BOOL) textFieldShouldClear:(UITextField *)textField{
    [textField resignFirstResponder];   
    return YES;
}

这种方法的缺点是,如果您想阻止清除文本字段,您的代码就会变得混乱。相反,您可以尝试定义一个自定义方法,然后将其连接到 valueDidChange 方法并检查是否有空值。

-(IBAction)hideKeyboardFromTextField:(id)sender{
  //TODO: Check if the previous value was longer than one character to differentiate
  //between backspace and clear. 

  //check if the editing caused the box to be empty
  if([[sender value] isEqualToString:@""] || [sender value] == nil)
    [sender resignFirstResponder];
  }
}

这里的问题是,当 UITextField 中有一个字符时,您无法轻松区分点击清除按钮和点击删除按钮。

正如我在答案开头所说,这从一开始就是不可取的,而且正如这里的答案所示,实施起来并不容易。考虑到所涉及的困难以及它不会带来最佳用户体验的事实,我认为不值得这么麻烦。

Yes, there is, although I suspect that doing so would violate the Apple Human Interface Guidelines.

To do so, add the following method to your view controller's implementation file. Then make the view controller into your textfield's delegate.

- (BOOL) textFieldShouldClear:(UITextField *)textField{
    [textField resignFirstResponder];   
    return YES;
}

The downside to this approach is if you ever want to prevent the textfield from clearing, your code becomes messy. Instead you might try to define a custom method and then connect it to the valueDidChange method and check for an empty value.

-(IBAction)hideKeyboardFromTextField:(id)sender{
  //TODO: Check if the previous value was longer than one character to differentiate
  //between backspace and clear. 

  //check if the editing caused the box to be empty
  if([[sender value] isEqualToString:@""] || [sender value] == nil)
    [sender resignFirstResponder];
  }
}

The problem here is that you can't easily differentiate between a tap on the clear button and a tap on the delete button when there is one character in the UITextField.

As I said in the beginning of my answer, this is not advisable in the first place and as the answers here have shown, it is not so easy to implement. I don't think it's worth the hassle, considering the difficulty involved and the fact that it doesn't result in optimal user experience.

人海汹涌 2024-10-22 03:45:38

这段代码绝对适合我隐藏键盘,同时清除文本字段的内容

- (BOOL)textFieldShouldClear:(UITextField *)textField
{
    textField.text = @"";
    return NO;
}

This code is definitely working for me to hide the key board while clearing out the content of the textfield

- (BOOL)textFieldShouldClear:(UITextField *)textField
{
    textField.text = @"";
    return NO;
}
梦在深巷 2024-10-22 03:45:38

是的。调用 textFieldShouldClear: 方法。

Yep. Call resignFirstResponder on the text field in the delegate's textFieldShouldClear: method.

梦断已成空 2024-10-22 03:45:38

在 UITextFieldDelegate 中

- (BOOL)textFieldShouldClear:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

但是这样有一个问题。根据手册,“文本字段调用此方法来响应用户按下内置清除按钮。(默认情况下不显示此按钮,但可以通过更改文本字段的clearButtonMode属性中的值来启用。)当编辑开始并且文本字段的clearsOnBeginEditing 属性设置为YES 时,也会调用此方法。”

请注意,如果clearsOnBeginEditing 设置为YES,则在编辑开始时调用此方法。因此,如果您在此方法中调用 resignFirstResponder ,那么编辑实际上不会开始。所以你需要将clearsOnBeginEditing设置为NO。显然,编辑开始时文本字段不会被清除。

另一个与问题没有直接关系的重要事项。点击清除按钮后隐藏键盘并不是一种常见的行为,苹果也不喜欢改变标准项目的行为。您可能会因此被拒绝。

In UITextFieldDelegate

- (BOOL)textFieldShouldClear:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

But there is a problem with this. From the manual, "The text field calls this method in response to the user pressing the built-in clear button. (This button is not shown by default but can be enabled by changing the value in the clearButtonMode property of the text field.) This method is also called when editing begins and the clearsOnBeginEditing property of the text field is set to YES."

Note that, this method is called when editing begins if clearsOnBeginEditing is set to YES. So if you call resignFirstResponder in this method then editing will not begin actually. So you need to set clearsOnBeginEditing to NO. Obviously then the text field won't be cleared when editing begins.

Another IMPORTANT matter not directly related to the question. Hiding the keypad after tapping clear button is not a familiar behavior and Apple does NOT like changing the behavior of standard items. You may get a rejection for this.

请帮我爱他 2024-10-22 03:45:38

试试这个代码:

[TextField performSelector:@selector(resignFirstResponder) withObject:nil afterDelay:0.1];

Try this code:

[TextField performSelector:@selector(resignFirstResponder) withObject:nil afterDelay:0.1];
烟花易冷人易散 2024-10-22 03:45:38

对于 Swift

在你的 UITextFieldDelegate 中

func textFieldShouldClear(_ textField: UITextField) -> Bool {
    textField.resignFirstResponse()
    return true
}

For Swift

In your UITextFieldDelegate

func textFieldShouldClear(_ textField: UITextField) -> Bool {
    textField.resignFirstResponse()
    return true
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文