在空白文本字段中检测UITextField中的退格 [英] Detect backspace in UITextField on a blank textfield

查看:114
本文介绍了在空白文本字段中检测UITextField中的退格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

检测UITextField中的退格键

我正在尝试检测退格键在我的UITextfield上是空的。我看到了这个,但无法弄明白。任何精心设计的答案或代码段都会有所帮助

I am trying to detect the backspace key envent on my UITextfield which is empty. I saw this but could not figure it out. Any elaborate answer or code snippet would be helpful

检测UITextField中的退格键

推荐答案

我发现它不可能。

当UITextField为空时,不调用以下委托方法。

When the UITextField is empty, the following delegate method isn't called.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

我通过在字段中不断占用空间来征服它,当我检测到退格并且字段只包含空格时,我在委托方法中返回NO。

I conquered this by constantly having a space in the field, and when I detect a backspace and the field only contains a space, I return NO in the delegate method.

if ([string isEqualToString:@""] && [textField.text isEqualToString:@" "]){
    // Backspace called on 'empty' field.
    return NO;
}

在视觉上,这与空白字段一样好,这是一个解决方法未在空字段上调用委托方法。

Visually, this is as good as the field being empty, and it's a workaround to the delegate method not being called on an empty field.

希望有所帮助。

这篇关于在空白文本字段中检测UITextField中的退格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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