我有几个 UITextView 子视图,都使用相同的自定义输入界面(基本上是带有自动填充选项和保存按钮的数字键盘)。
我的问题是,当从我的自定义键盘修改文本字段的文本时,不会调用委托方法 shouldChangeCharactersInRange: (在将文本从剪贴板粘贴到文本字段以及使用标准数字键盘时,它确实有效)。文本字段的文本会更改,但不会调用防止无效条目的委托方法。其他风格 DidBeginEditing: 的委托方法总是被调用。
尽管在此SO LINK中说了什么,但文档指出将调用 shouldChangeCharactersInRange:委托方法:“每当用户键入新字符或删除现有字符时,文本视图都会调用此方法。”
我错过了什么?
相关代码部分:
视图控制器.h:
@interface ManualPositionViewController : UIViewController <UITextFieldDelegate> {
LocationEntryTextField *latitude;
}
@property (nonatomic, retain) IBOutlet LocationEntryTextField *latitude;
@property (nonatomic, retain) IBOutlet LocationKeyboard *locationKeyboard;
..
视图控制器.m:
@synthesize latitude;
@synthesize locationKeyboard;
self.latitude.inputView = locationKeyboard;
self.latitude.delegate = self;
- (void)textFieldDidBeginEditing:(LocationEntryTextField *)aTextField {
NSLog(@"textFieldDidBeginEditing called!");
self.locationKeyboard.currentTextfield = aTextField;
}
- (BOOL)textField:(LocationEntryTextField *)editedTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)replacementString {
NSLog(@"shouldChangeCharactersInRange called!");
NSCharacterSet *decimalSet = [NSCharacterSet decimalDigitCharacterSet];
if ([[replacementString stringByTrimmingCharactersInSet:decimalSet] isEqualToString:@""]) {
NSLog(@"Result: YES");
return YES;
}
else {
NSLog(@"Result: NO");
return NO;
}
}
位置键盘.h:
#import <UIKit/UIKit.h>
#import "LocationEntryTextField.h"
@interface LocationKeyboard : UIView {
LocationEntryTextField *currentTextfield; // track first responder
}
@property (weak) LocationEntryTextField *currentTextfield;
- (IBAction) numberButtonPressed:(UIButton*)sender;
- (IBAction) backspaceButtonPressed:(UIButton*)sender;
@end
- (IBAction) numberButtonPressed:(UIButton*)sender {
NSString *entryString = @"test";
[self.currentTextfield replaceRange:self.currentTextfield.selectedTextRange withText:entryString];
}
LocationEntryTextField.h:
@interface LocationEntryTextField : UITextField
..