捕获 Backspace ,这是一个好的解决方案吗?
我很难捕获 UITextView 中的退格按钮。 方法来捕获它
- (BOOL)textView:(UITextView *)textView
shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
我试图用我认为可以这样做的
if([text isEqualToString:@"\b") { // code ... }
。但由于某种原因,当按下退格键时,“文本”为空。
我知道我可以比较 UITextView 的长度,但这不是我想要实现的。
所以我找到了解决这个问题的方法。 如果我查看 '[text length]' 默认键盘上的每个键都会返回 >0 。除了退格键之外的每个键都是 0。这样我就知道何时按下退格键。
我做这个检查。
if([text lenght] == 0) { // BACKSPACE PRESSED }
您对此有何看法? -或者我可以用更好的方式来做吗?
编辑:正如 David Gelhar 告诉我的那样,这不会起作用,因为 CUT 也会触发 ([text lenght] == 0) 。
有人可以在这里指出正确的方向吗?
问候。 - 马丁
I having a hard time to capture the backspace button in a UITextView.
I am trying to capture it in the method
- (BOOL)textView:(UITextView *)textView
shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
I thought it was ok to do like this.
if([text isEqualToString:@"\b") { // code ... }
But for some reason, when backspace is pressed 'text' is empty.
I know I can compare lenght of the UITextView but it isnt what I want to achieve.
So I found a solution to this.
If I look at '[text lenght]' every key on the defaultkeyboard returns >0 . Every key excepts the backspace wich is 0. In that way i know when backspace is pressed.
I do this check.
if([text lenght] == 0) { // BACKSPACE PRESSED }
What is your opinion about this? -or can I do it in a better way?
EDIT: As David Gelhar told me, this wont work cus CUT will trigger ([text lenght] == 0) aswell.
Anyone that can point me to the correct directions here?
Regards.
- Martin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您还应该测试用户执行 CUT 时会发生什么...
You should also test what happens when the user does a CUT...