如何获取UITextFieldDelegate.shouldChangeCharactersInRange以自定义inputView触发? [英] How can I get UITextFieldDelegate.shouldChangeCharactersInRange to fire with custom inputView?
问题描述
我有一个自定义键盘,用于编辑 mytextField
中的文本,它的效果非常好。但是,我不能使用我的自定义键盘来执行 shouldChangeCharactersInRange
委托。当我使用我的实际键盘(显然不是默认的iPhone键盘,因为我设置mytextField.inputView = numberPad.view),它确实执行。我应该如何使用自定义键盘启动 shouldChangeCharactersInRange
委托? BTW,自定义键盘只是一堆按钮。
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];
}
推荐答案
你永远不会得到 shouldChangeCharactersInRange
用自定义键盘调用。
You will never get the shouldChangeCharactersInRange
called with a custom keyboard.
你可以得到的是事件 UIControlEventEditingChanged
UITextField
。因此,不要依赖 shouldChangeCharactersInRange
方法(此外,该方法也被称为bug),所以您应该依赖此事件,每次用户更改内容文本字段。
What you can get is the event UIControlEventEditingChanged
of the UITextField
. So instead of relying on shouldChangeCharactersInRange
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.
[textField addTarget:self action:@selector(myTextfieldChangedMethod:) forControlEvents:UIControlEventEditingChanged];
如果您使用自定义键盘修改自己的文本字段,只需调用此方法通知所有侦听器事件 UIControlEventEditingChanged
。
If you modify yourself the text field with your custom keyboard, just call this method to notify all listeners of the event UIControlEventEditingChanged
.
[textField sendActionsForControlEvents:UIControlEventTouchUpInside];
这篇关于如何获取UITextFieldDelegate.shouldChangeCharactersInRange以自定义inputView触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!