How can I get UITextFieldDelegate.shouldChangeCharactersInRange to fire with custom inputView?
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;
mytextFie开发者_运维技巧ld.delegate = self;
[mytextField becomeFirstResponder];
}
You will never get the shouldChangeCharactersInRange
called with a custom keyboard.
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];
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];
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:
- (IBAction) numberPadPressed: (UIButton *)sender {
NSString *newText = [[mytextField text] stringByReplacingCharactersInRange:[mytextField selectedRange] withString:string];
[mytextField setText:newText];
}
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.
精彩评论