未使用自定义键盘调用UItextView委托

时间:2012-11-09 00:13:47

标签: iphone ios uitextfield uitextfielddelegate

我有几个UITextView子视图,都使用相同的自定义输入界面(基本上是带有自动填充选项和保存按钮的数字键盘)。

我的问题是,当从我的自定义键盘修改文本字段的文本时,不会调用委托方法shouldChangeCharactersInRange:当将文本从剪贴板粘贴到文本字段时以及使用标准数字键盘时,它确实有效。文本字段的文本更改,但不调用用于防止无效条目的委托方法。风格DidBeginEditing的其他委托方法:始终被调用。

尽管在SO LINK documentation中声明了将调用shouldChangeCharactersInRange:delegate方法:“只要用户键入新字符或删除现有字符,文本视图就会调用此方法字符。”

我错过了什么?

相关代码部分:

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
..

0 个答案:

没有答案