iPhone:可以使用 UITextField 清除按钮关闭键盘吗?

发布于 2024-09-12 17:27:31 字数 166 浏览 3 评论 0原文

我想知道是否有办法让 UITextField 清除按钮“始终可见”

textfield.clearButtonMode = UITextFieldViewModeAlways;

似乎不起作用 ..是否可以使用按钮关闭键盘?

提前致谢。

I'm wondering if there is a way to have the UITextField clear button 'always visible'

textfield.clearButtonMode = UITextFieldViewModeAlways;

doesn't seem to work
.. and if it is possible to dismiss the keyboard using the button?

Thanks in advance.

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

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

发布评论

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

评论(5

你另情深 2024-09-19 17:27:31

就像之前提到的,苹果似乎在清除字段后设置文本字段焦点。

解决方案非常简单。自己清空字段即可,resignFirstResponder并返回NO

    -(BOOL)textFieldShouldClear:(UITextField *)textField
    {
        textField.text = @"";
        [textField resignFirstResponder];

        return NO;
    }

Like mentioned before it seems apple is setting the textfield focus after you clear the field.

The solution is quite simple. Just clear the field yourself, resignFirstResponder and return NO

    -(BOOL)textFieldShouldClear:(UITextField *)textField
    {
        textField.text = @"";
        [textField resignFirstResponder];

        return NO;
    }
沫尐诺 2024-09-19 17:27:31

该函数

- (BOOL)textFieldShouldClear:(UITextField *)textField

在您的委托中,当用户想要清除文本字段时调用 。如果你返回 YES 并调用

[textField resignFirstResponder];

键盘应该消失。我不知道clearButtonMode,除了您可能想尽早设置它,最好是在将视图添加到其超级视图之前。

编辑为了确保您确实辞去了响应者的职务,请稍后尝试执行此操作:

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

In your delegate, the function

- (BOOL)textFieldShouldClear:(UITextField *)textField

is called when the users wants to clear the textfield. If you return YES and call

[textField resignFirstResponder];

the keyboard should go away. I don't know about the clearButtonMode, other than that you may want to set it early, preferably before adding the view to its superview.

edit To make sure you really resign the responder, try doing it just a little later:

[textField performSelector:@selector(resignFirstResponder) withObject:nil afterDelay:0.1];
风铃鹿 2024-09-19 17:27:31

延迟对我来说效果不太好。相反,我向委托添加了一个实例变量:

BOOL cancelEdit;

然后在委托实现中:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (cancelEdit) {
        cancelEdit = NO;

        return NO;
    } else {
        return YES;
    }
}

- (BOOL)textFieldShouldClear:(UITextField *)textField
{
    cancelEdit = YES;

    return YES;
}

The delay didn't work well for me. Instead I added an instance variable to the delegate:

BOOL cancelEdit;

Then in the delegate implementation:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (cancelEdit) {
        cancelEdit = NO;

        return NO;
    } else {
        return YES;
    }
}

- (BOOL)textFieldShouldClear:(UITextField *)textField
{
    cancelEdit = YES;

    return YES;
}
誰認得朕 2024-09-19 17:27:31

UITextFieldDelegate textFieldShouldClear

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

http://developer.apple.com/iphone/library/documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html#//apple_ref/occ/intfm/UITextFieldDelegate/textFieldShouldClear

UITextFieldDelegate textFieldShouldClear

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

http://developer.apple.com/iphone/library/documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html#//apple_ref/occ/intfm/UITextFieldDelegate/textFieldShouldClear:

旧时浪漫 2024-09-19 17:27:31

我发现这种奇怪的行为是由竞争手势识别器引起的,该手势识别器在调用 textFieldShouldClear: 之前放弃了键盘之前的第一响应者。它似乎正在腐蚀第一响应者。

如果您以这种方式进行设置,请确保手势识别器上的cancelsTouchesInView 设置为YES。这样您就不需要在 textFieldShouldClear: 或 textFieldShouldBeginEditing: 委托方法中执行任何特殊操作。

I discovered this odd behavior was caused by a competing gesture recognizer that resigned the first responder before the keyboard before textFieldShouldClear: was called. It seemed to be corrupting the first responder.

If you've set it up this way, ensure that cancelsTouchesInView on your gesture recognizer is set to YES. This way you shouldn't need to do anything special in the textFieldShouldClear: or textFieldShouldBeginEditing: delegate methods.

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