如何模拟UITextField上的删除键? [英] How to simulate the delete key on UITextField?

查看:186
本文介绍了如何模拟UITextField上的删除键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iPad上创建一个应用程序。我使用 UITextField inputView 属性创建一个自定义键盘。要在光标位置插入文本,我使用复制&粘贴方法( http://dev.ragfield .com / 2009/09 / insert-text-at-current-cursor-location.html ),工作正常。现在,我想创建删除键。我使用的代码是:

I'm creating an application on the iPad. I create a custom keyboard using UITextField's inputView property. To insert text at the cursor position, I use the copy & paste method (http://dev.ragfield.com/2009/09/insert-text-at-current-cursor-location.html), which works fine. Now, I want to create the delete key. The code that I'm using is:

if (textField.text.length > 0) {
    textField.text = [textField.text substringToIndex:textField.text.length-1];
}

但是,你可能知道,它只删除最后一个字符光标为。有人知道更好的解决方案吗?

However, as you may know, it only deletes the last character no matter where the cursor is. Does anyone know a better solution?

非常感谢!

推荐答案

p>只需调用[textField deleteBackward]。它负责处理光标位置和高亮显示。

Just call [textField deleteBackward]. It takes care of cursor position and highlight for you.

这篇关于如何模拟UITextField上的删除键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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