当点击uitextfield的清除button键盘消失而不是文本

在iPhone中,我有它的UITextField查看。当我点击UITextField的键盘上的清除button正在消失,而不是UITextField的文本。在iPad中,一切正常。 请build议我,该怎么办? 提前致谢!!

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

 Note: set Attributes inspector property of UITextField 

清除button – >编辑时出现

 so it will display 'X' button while editing in textfield. 

// Objective-C

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

// Swift

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

在附上uitextifield的委托后,尝试执行此代码

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

首先,检查所有与你的UITextField相关的代码块(特别是代码yourTextField.hidden = YES;

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

textFieldDidEndEditingtextFieldShouldEndEditingtextFieldShouldReturn.etc

要么

实现textFieldShouldClear委托,并在这里写代码可见并清除你的UITextField

要做到这一点,你必须如下设置clearButtonMode

 yourTextField.clearButtonMode = UITextFieldViewModeWhileEditing; yourTextField.delegate = self; //For active keyboard again [yourTextField becomeFirstResponder]; 

然后执行textFieldShouldClear委托

YourClass.h

  @interface className : UIViewController <UITextFieldDelegate> 

YourClass.m

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

只要确定我给了这两个

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

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

你用这种方法做了些什么?

也许你是在这个委托方法调用resignFirstResponder,这就是为什么键盘被解雇。

请通过委托方法,并检查你在做什么。

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

 yourTextField.clearButtonMode = UITextFieldViewModeNever; 

选中此行并将其删除或更改查看模式..