4

いくつかの 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
..
4

1 に答える 1

8

この行:

[self.currentTextfield replaceRange:self.currentTextfield.selectedTextRange withText:entryString];

への呼び出しにはなりませんtextField:shouldChangeCharactersInRange:replacementString:。それはあなたが期待していることですか?

テキスト フィールドのテキストを明示的に変更しているため、「入力」は行われません。

カスタム キーボードでテキスト フィールドを更新する適切な方法は、「insertText:」メソッドを呼び出すことです。このメソッドは、選択、カーソルの移動、およびデリゲート メソッドの呼び出しを適切に処理します。

編集:完全なカスタム キーボード セットアップ (実際のボタンを除く) については、こちらの回答を参照してください。

于 2012-11-09T00:23:57.600 に答える