如何让 UITextFieldDelegate.shouldChangeCharactersInRange 使用自定义 inputView 触发?
我有一个自定义键盘,用于编辑 mytextField
中的文本,效果很好。但是,我永远无法让 shouldChangeCharactersInRange
委托使用我的自定义键盘执行。当我使用我的实际键盘时(显然不是默认的 iPhone 键盘,因为我设置了 mytextField.inputView = numberPad.view),它确实会执行。我应该怎么做才能使用自定义键盘触发 shouldChangeCharactersInRange
委托?顺便说一句,自定义键盘只是一堆按钮。
- (void) numberPadPressed: (UIButton *)sender {
[mytextField insertText:[[NSNumber numberWithInt:sender.tag] stringValue]];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *resultingString = [textField.text stringByReplacingCharactersInRange: range withString: string];
NSLog(@"shouldChangeCharactersInRange: %@", resultingString);
return true;
}
- (void)viewDidLoad {
mytextField.text = @"";
mytextField.inputView = numberPad.view;
mytextField.delegate = self;
[mytextField becomeFirstResponder];
}
I have a custom keyboard that I'm using to edit the text in mytextField
and it works great. However, I can never get the shouldChangeCharactersInRange
delegate to execute using my custom keyboard. It does execute when I use my actual keyboard(obviously not the default iPhone keyboard, since I'm set mytextField.inputView = numberPad.view). What should I do to cause the shouldChangeCharactersInRange
delegate to fire using the custom keyboard? BTW, the custom keyboard is just a bunch of buttons.
- (void) numberPadPressed: (UIButton *)sender {
[mytextField insertText:[[NSNumber numberWithInt:sender.tag] stringValue]];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *resultingString = [textField.text stringByReplacingCharactersInRange: range withString: string];
NSLog(@"shouldChangeCharactersInRange: %@", resultingString);
return true;
}
- (void)viewDidLoad {
mytextField.text = @"";
mytextField.inputView = numberPad.view;
mytextField.delegate = self;
[mytextField becomeFirstResponder];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您永远不会使用自定义键盘调用
shouldChangeCharactersInRange
。你能得到的是
UITextField
的事件UIControlEventEditingChanged
。因此,您应该依赖此事件,而不是依赖shouldChangeCharactersInRange
方法(已知该方法还存在错误),每次用户更改文本字段的内容时都会触发该事件。如果您使用自定义键盘修改自己的文本字段,只需调用此方法即可通知
UIControlEventEditingChanged
事件的所有侦听器。You will never get the
shouldChangeCharactersInRange
called with a custom keyboard.What you can get is the event
UIControlEventEditingChanged
of theUITextField
. So instead of relying onshouldChangeCharactersInRange
method (which is known to be buggy in addition), you should rely on this event, that will be fired each time the user changes the content of the text field.If you modify yourself the text field with your custom keyboard, just call this method to notify all listeners of the event
UIControlEventEditingChanged
.编辑:抱歉,之前误解了您的问题。
当您的文本字段从系统键盘获取输入时,它会调用
shouldChangeCharactersInRange
,让您有机会确定文本是否应该实际显示(例如,您可以实现它来识别快捷方式并插入一些较长的文本)。但文本字段无法将自定义视图中的输入识别为文本输入。如果您有自定义 inputView,则无需调用此方法 - 只需直接从 IBAction 更新文本字段即可:
Edit: sorry, misunderstood your question previously.
Your text field calls
shouldChangeCharactersInRange
when it gets input from the system keyboard, giving you an opportunity to determine whether the text should actually display (for example you could implement it to recognize a shortcut and insert some longer text). But the text field doesn't recognize input from your custom view as text input.If you have a custom inputView, you don't need to invoke this method--just update the text field directly from your IBAction:
我知道这是一个老问题,但我在需要支持 5.0 之前的设备时遇到了同样的问题。如果您覆盖 insertText: 按照 Rob 的说明 http://dev.ragfield.com/2009/09/insert-text-at-current-cursor-location.html 然后你每次都会触发shouldChangeCharactersInRange。
如果您需要像我一样支持退格键,您可以使用 @cormacrelf 的答案 如何模拟 UITextField 上的删除键?
我无法解决的唯一问题是,当您从字符串中间删除一个字符时,光标跳回到末尾字符串的。
I know this is an old question but I ran into the same problem in needing to support pre 5.0 devices. If you override insertText: by following the instructions from Rob at http://dev.ragfield.com/2009/09/insert-text-at-current-cursor-location.html then you will get shouldChangeCharactersInRange to fire every time.
If you then need to support the backspace key like I did you can use the answer from @cormacrelf at How to simulate the delete key on UITextField?
The only snafu I haven't been able to work around is that when you delete a character from the middle of the string the cursor jumps back to the end of the string.