捕获 Backspace ,这是一个好的解决方案吗?

发布于 2024-08-30 17:28:12 字数 706 浏览 3 评论 0原文

我很难捕获 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枕头说它不想醒 2024-09-06 17:28:12

您还应该测试用户执行 CUT 时会发生什么...

You should also test what happens when the user does a CUT...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文