如何检测 UIKeyboard“退格键” UITextField 上的按钮触摸?
如何检测 UIKeyboard
上按下的退格按钮?
谢谢
编辑:是否有任何键盘委托返回按键值?
How can i detect backspace button pressed on UIKeyboard
?
thanks
EDIT: Is there any keyboard delegate that returns key pressed value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果 UITextField 为空,则不调用 shouldChangeTextInRange 委托方法
您可以子类化 UITextField 并重写此方法:
由于 UITextField 符合 UITextInput 协议,因此实现了此方法,并且您可以重写它以检测何时按下退格键。
然后,您可以编写自己的协议/委托方法,以在检测到退格键时提醒您的自定义文本字段委托。
if the UITextField is empty, the shouldChangeTextInRange delegate method is not called
You can subclass UITextField and override this method:
since UITextField is compliant to the UITextInput protocol, this method is implemented, and you can override it to detect when backspace is pressed.
Then, you can write your own protocol/delegate method to alert your custom textfield delegate if the backspace is detected.
首先,引用
UITextField
的UIViewController
需要符合UITextFieldDelegate
协议
。然后将您的类设置为UITextField
的delegate
(例如[myTextField setDelegate:self]
)。然后在您的班级中添加以下内容。当 "" 字符串作为replacementString
发送时,您知道它是退格键。First of all your
UIViewController
that references theUITextField
needs to conform to theUITextFieldDelegate
protocol
. Then set your class asdelegate
to theUITextField
(eg.[myTextField setDelegate:self]
). Then in your class you add the following. When a string of "" is sent as thereplacementString
you know it's backspace.我暂时
在退格键上的
textFieldDidBeginEditing
上插入一个零空格字符,它删除了该字符,但从图形上看是相同的!这是一个黑客,它有效,但它隐藏了占位符文本......不好......
Temporary i'm inserting a zero space char on
textFieldDidBeginEditing
on backspace, it remove that chars, but graphically it's the same!
It's an hack, it works, but it hide placeholder text... not good...