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

标签 ios objective-c iphone keyboard uitextfield

在 iPhone 中,我有一个包含 UITextField 的 View 。当我点击 UITextField 的清除按钮时,键盘消失了,而不是清除 UITextField 中的文本。在 iPad 上它工作正常。我该怎么做才能解决这个问题?

最佳答案

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

注意:设置 UITextField 的 Attributes inspector 属性

Clear Button -> Appears while editing

因此在文本字段中编辑时会显示清除按钮。

// objective-C

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

// swift

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

关于ios - 点击 TextField 清除按钮隐藏键盘而不是清除文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11337961/

相关文章:

ios - Facebook 请求 FBWebDialog UI 看起来很奇怪

ios - 在 iOS 中录制音频并永久保存

ios - fetchRequest 返回空数组

iphone - 带注释的 map 边界框

ios - 解决深度嵌套的 View Controller 层次结构

ios - 在 View Controller 中播放视频

objective-c - 将 NSDictionary 中的每个 NSArray 更改为 Mutable

iphone - 使用 NEON 指令进行图像阈值处理

iphone - 禁用自定义选择器中的特定行

ios - iOS UITableView如何在NavigationBar下使用?