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

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

问题描述

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.


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];

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


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.

解决方案

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天全站免登陆