点击UITextField的清除按钮可隐藏键盘,而不是清除文本 [英] Tap on UITextField's clear button hides keyboard instead of clearing text

查看:331
本文介绍了点击UITextField的清除按钮可隐藏键盘,而不是清除文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iPhone中,我有一个具有UITextField的视图.当我点击UITextField的清除按钮时,键盘将关闭,而不是清除UITextField中的文本.在iPad上,它可以正常工作.我该怎么做才能解决此问题?

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?

推荐答案

只需清除字段resignFirstResponder(如果要隐藏键盘),然后返回NO/false

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

注意:设置UITextField

清除按钮->编辑时显示

因此它将在文本字段中进行编辑时显示清除按钮.

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

//Objective-C

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

//Swift

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

这篇关于点击UITextField的清除按钮可隐藏键盘,而不是清除文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆