ios 点击 UITextField 的清除按钮隐藏键盘而不是清除文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11337961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:08:15  来源:igfitidea点击:

Tap on UITextField's clear button hides keyboard instead of clearing text

iosobjective-ciphonekeyboarduitextfield

提问by Mad

In iPhone, I have a view which has a UITextField. When I tap on the clear button of UITextField's the keyboard dismissed instead of clearing the text in the UITextField. On an iPad it is working correctly. What can I do to fix this?

在 iPhone 中,我有一个视图,其中包含UITextField. 当我点击UITextField's的清除按钮时,键盘关闭而不是清除UITextField. 在 iPad 上它工作正常。我能做些什么来解决这个问题?

回答by Himanshu padia

Just clear the field, resignFirstResponder(if you want to hide keyboard) and return NO/false

只需清除该字段,resignFirstResponder(如果您想隐藏键盘)并返回NO/false

Note: set Attributes inspector property of UITextField

注意:设置属性检查器属性 UITextField

Clear Button -> Appears while editing

清除按钮 -> 编辑时出现

so it will display the clear button while editing in the text field.

所以它会在文本字段中编辑时显示清除按钮。

// Objective-C

// 目标-C

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

// Swift

// 斯威夫特

func textFieldShouldClear(textField: UITextField) -> Bool {
    textField.text = ""
    textField.resignFirstResponder()
    return false
}

回答by Ajay Malviya

Try this code after you attach delegate of uitextifield

在附加 uitextifield 的委托后尝试此代码

-(BOOL)textFieldShouldClear:(UITextField *)textField
{
     return true;
}

回答by Shamsudheen TK

First, check all the code blocks that related to your UITextField(especially the code yourTextField.hidden = YES;)

首先,检查与您的UITextField(尤其是代码yourTextField.hidden = YES;)相关的所有代码块

Put break points and analyze every UITextFielddelegates that you implemented.

放置断点并分析UITextField您实现的每个委托。

(textFieldDidEndEditing,textFieldShouldEndEditing,textFieldShouldReturn.etc.)

( textFieldDidEndEditing, textFieldShouldEndEditing, textFieldShouldReturn.etc.)

OR

或者

Implement the textFieldShouldCleardelegate and write the code here to visible and clear your UITextField

实现textFieldShouldClear委托并在此处编写代码以显示和清除您的UITextField

To do this, you have to set the clearButtonModeas below,

为此,您必须设置clearButtonMode如下,

yourTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
yourTextField.delegate = self;

//For active keyboard again
[yourTextField becomeFirstResponder];

Then implement the textFieldShouldCleardelegate

然后实现textFieldShouldClear委托

YourClass.h

你的课堂.h

    @interface className : UIViewController <UITextFieldDelegate>

YourClass.m

你的课堂.m

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

回答by Sj.

Just make sure U've given these two

只要确保你已经给了这两个

editingTextField.delegate = self;
editingTextField.clearButtonMode = UITextFieldViewModeWhileEditing;

TextFieldShouldClearis needed only if you need to do some customizations :-)

仅当您需要进行一些自定义时才需要TextFieldShouldClear:-)

Are you doing some thing in this method?

你在用这个方法做些什么吗?

Maybe you are are calling resignFirstResponder in this delegate method, thats why the keyboard is getting dismissed.

也许您正在此委托方法中调用 resignFirstResponder,这就是键盘被关闭的原因。

Please go through the delegate methods, and check what u r doing exactly.

请仔细检查委托方法,并检查您在做什么。

回答by Alessandro Ornano

This issue happened also if you have

如果你有这个问题也会发生

yourTextField.clearButtonMode = UITextFieldViewModeNever;

Check this line and delete it or change view mode..

检查此行并将其删除或更改视图模式..