いくつかの UITextView サブビューがあり、すべて同じカスタム入力インターフェイス (基本的には自動入力オプションと保存ボタンを備えたテンキー) を使用しています。
私の問題は、デリゲート メソッド shouldChangeCharactersInRange: が、テキスト フィールドのテキストがカスタム キーボードから変更されたときに呼び出されないことです (テキストをクリップボードからテキスト フィールドに貼り付けた場合や、標準のテンキー キーボードを使用した場合にも機能します)。テキストフィールドのテキストは変更されますが、無効なエントリを防止するデリゲート メソッドは呼び出されません。その他のスタイル DidBeginEditing: のデリゲート メソッドは常に呼び出されます。
このSO LINKで述べられていることにもかかわらず、ドキュメントには shouldChangeCharactersInRange: デリゲート メソッドが呼び出されると記載されています。
私は何が欠けていますか?
関連するコード部分:
ViewController.h:
@interface ManualPositionViewController : UIViewController <UITextFieldDelegate> {
LocationEntryTextField *latitude;
}
@property (nonatomic, retain) IBOutlet LocationEntryTextField *latitude;
@property (nonatomic, retain) IBOutlet LocationKeyboard *locationKeyboard;
..
ViewController.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;
}
}
LocationKeyboard.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
..