检测空UITextField中的退格 [英] Detect backspace in empty UITextField

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

问题描述

有没有办法检测何时在 UITextField Backspace / 删除键>那是空的?我想知道只有当 UITextField 为空时才按下 Backspace

Is there any way to detect when the Backspace/Delete key is pressed in the iPhone keyboard on a UITextField that is empty? I want to know when Backspace is pressed only if the UITextField is empty.

根据@Alex Reynolds在评论中提出的建议,我在创建文本字段时添加了以下代码:

Based on the suggestion from @Alex Reynolds in a comment, I've added the following code while creating my text field:

[[NSNotificationCenter defaultCenter] addObserver:self
          selector:@selector(handleTextFieldChanged:)
              name:UITextFieldTextDidChangeNotification
            object:searchTextField];

收到此通知( handleTextFieldChanged 功能是当我在空字段中按 Backspace 键时,仍然不会。有什么想法?

This notification is received (handleTextFieldChanged function is called), but still not when I press the Backspace key in an empty field. Any ideas?

这个问题似乎有些混乱。我想在按下 Backspace 键时收到通知。而已。但是当 UITextField 已经为空时,解决方案也必须正常工作。

There seems to be some confusion around this question. I want to receive a notification when the Backspace key is pressed. That's it. But the solution must also work when the UITextField is already empty.

推荐答案

这可能是一个长镜头,但它可以工作。尝试将文本字段的文本设置为零宽度空格字符 \ u200B 。当在显示为空的文本字段上按下退格键时,它实际上会删除您的空间。然后你可以重新插入空间。

This may be a long shot but it could work. Try setting the text field's text to a zero width space character \u200B. When backspace is pressed on a text field that appears empty, it will actually delete your space. Then you can just reinsert the space.

如果用户设法将插入符号移动到空格的左边,则可能无效。

May not work if the user manages to move the caret to the left of the space.

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

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