gpt4 book ai didi

iphone - 未使用自定义键盘调用 UItextView 委托(delegate)

转载 作者:可可西里 更新时间:2023-11-01 17:09:45 25 4
gpt4 key购买 nike

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

我的问题是委托(delegate)方法 shouldChangeCharactersInRange: 当从我的自定义键盘修改文本字段的文本时不会调用委托(delegate)方法(当将文本从剪贴板粘贴到文本字段时以及使用标准数字键盘时它确实有效)。文本字段的文本发生变化,但不会调用防止无效输入的委托(delegate)方法。其他样式为 DidBeginEditing: 的委托(delegate)方法总是被调用。

尽管这 SO LINK 中说了什么documentation指出将调用 shouldChangeCharactersInRange: 委托(delegate)方法:“每当用户键入新字符或删除现有字符时, TextView 都会调用此方法。”

我错过了什么?

相关代码部分:

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;
}
}

位置键盘.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
..

最佳答案

这一行:

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

不会导致调用 textField:shouldChangeCharactersInRange:replacementString:。这就是您所期待的吗?

由于您正在显式更改文本字段的文本,因此不会进行“键入”。

让自定义键盘更新文本字段的正确方法是调用“insertText:”方法。此方法将正确处理任何选择、移动光标和调用委托(delegate)方法。

编辑:您不妨看看 my answer here用于完整的自定义键盘设置(减去实际按钮)。

关于iphone - 未使用自定义键盘调用 UItextView 委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13300176/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com