【发布时间】:2012-11-09 00:13:46
【问题描述】:
我有几个 UITextView 子视图,都使用相同的自定义输入界面(基本上是带有自动填充选项和保存按钮的数字键盘)。
我的问题是委托方法 shouldChangeCharactersInRange: 在从我的自定义键盘修改文本字段的文本时不会被调用(在将文本从剪贴板粘贴到文本字段以及使用标准数字键盘时它确实有效)。文本字段的文本会更改,但不会调用防止无效条目的委托方法。其他样式 DidBeginEditing: 的委托方法总是被调用。
尽管在SO LINK 中说了什么,documentation 声明将调用 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
..
【问题讨论】:
标签: iphone ios uitextfield uitextfielddelegate