过滤输入UITextField的字符 [英] Filtering characters entered into a UITextField

查看:79
本文介绍了过滤输入UITextField的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个UITextField。我想限制可以输入到字段中的字符集到我定义的集合。当使用UITextFieldDelegate方法提交文本时,我可以过滤输入到字段中的字符:

I have a UITextField in my application. I'd like to restrict the set of characters that can be can be entered into the field to a set that I have defined. I could filter the characters entered into the field when the text is committed using the UITextFieldDelegate method:

- (BOOL)textFieldShouldReturn:(UITextField*)textField

但是,这会给用户留下错误的印象,因为尽管从最终值,在按Return / Done / etc之前,它们仍然可见地进入文本字段。在键盘上选择限制字符时,哪种方法可以阻止限制字符出现在文本字段中?

However, this gives the user a false impression as although restricted characters are removed from the final value, they were still visibly entered into the text field before pressing Return/Done/etc. What is the best approach that would prevent restricted characters appearing in the text field as they are selected on the keyboard?

注意:我正在运营假设我几乎无法控制iPhone键盘提供哪些键。我知道我可以在各种键盘实现之间切换,但我的印象是我无法禁用特定键。这个假设可能不正确。

Note: I am operating under the assumption that I have little control over which keys are provided by the iPhone keyboard(s). I am aware that I can switch between various keyboard implementations but am under the impression that I can't disable specific keys. This assumption may be incorrect.

推荐答案

查看 textField:shouldChangeCharactersInRange

只要键入新字符或从文本字段中删除现有字符, UITextFieldDelegate 就会调用此方法。您可以返回NO以不允许更改。

This method is called by the UITextFieldDelegate whenever new characters are typed or existing characters are deleted from the text field. You could return NO to not allow the change.

这篇关于过滤输入UITextField的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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