如何在 UITextField 中显示用户输入的字符之前访问它
我正在编写应用程序,其中我需要使用某种方法逐个字符地验证用户输入到 UITextField
的文本。
困难的是客户端希望在用户看到 UITextField 中的字符之前完成所有验证,因为可能存在他的服务器应用程序不支持 '$'
符号的情况,所以在我的验证方法中我应该将其替换为 'USD'
字符串 - 并且他不希望用户立即看到 '$'
,而只看到 'USD'
。
我知道诸如 UIControlEventEditingChanged
等事件,但我仍然不知道两件事:
如何在
UITextField
中看到用户输入的字符之前访问它在那里执行验证如何“即时”替换该字符并将其手动放置到UITextField (但我想我只是将其附加到
[[textField] text]
NSString
提前感谢您的帮助:)
I'm writing application in which I need to validate text entered by user to UITextField
, char by char with some method.
The difficult thing is that client wants to do all the validation before user see character in the UITextField because there might be situation that his server application doesn't support '$'
sign, so in my validation method I should replace it with 'USD'
string - and he doesn't want user to see '$'
, just 'USD'
immediately.
I know about events like UIControlEventEditingChanged
etc., but still, I don't know 2 things:
how to access character typed by user before it's seen in
UITextField
and execute validation therehow to subtitute this character 'on the fly' and put it manually to UITextField (but I suppose I'll just append this to
[[textField] text]
NSString
Thank You in advance for any help :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实现
UITextFieldDelegate
方法textField:shouldChangeCharactersInRange:replacementString:
,例如:Implement the
UITextFieldDelegate
methodtextField:shouldChangeCharactersInRange:replacementString:
, e.g.:类似于 omz 的答案,但如果您需要更完整的代码:
Similar to omz's answer but more complete code if you need: