iOS:如何将密钥发送到UITextField?

dak*_*ojo 8 objective-c uitextfield ios

我在iOS应用程序中实现了自定义键盘视图.我有几个使用此键盘的UITextField.其中一些UITextField具有覆盖的委托shouldChangeCharactersInRange.但是,如果我的键盘只在文本字段中设置文本值,则不会发送shouldChangeCharactersInRange消息.我认为我需要的是实际执行类似SendKey的操作并将密钥代码发送到UITextField.

有什么建议?

Dan*_*ark 5

正如我在这里回答中所说,UITextField符合UITextInput协议.所以你调用它的方法:

[textField replaceRange:textField.selectedTextRange withText:text];
Run Code Online (Sandbox Code Playgroud)

有趣的是:""将执行退格,当然," "还会做一个空间.

这应该shouldChangeCharactersInRange自动调用该方法.如果没有,你必须自己做.

如果要在iOS上创建自定义键盘,您肯定需要这样做.


BHS*_*key -1

听起来键盘负责将 shouldChangeCharactersInRange 消息发送到 UITextField 本身,而不仅仅是设置其值。range让你的键盘计算和的值string,然后调用:

if ([[theTextField delegate] textField:theTextField shouldChangeCharactersInRange:range replacementString:string])
    theTextField.text = [theTextField.text stringByReplacingCharactersInRange:range withString:string];
Run Code Online (Sandbox Code Playgroud)