[英]iOS: Detect backspace in empty UITextField with UITextField subclass
The clear answer to detecting a backspace is by creating a subclass of UITextField and overriding the deleteBackward
property. 检测退格的明确答案是通过创建UITextField的子类并覆盖deleteBackward
属性。
I've created a subclass a UITextField subclass, but am getting this error: 我已经创建了一个子类UITextField子类,但是出现了这个错误:
'NSInvalidArgumentException', reason: '-[UITextField setMyDelegate:]: unrecognized selector sent to instance
This is my subclass code: 这是我的子类代码:
My TextField.h: 我的TextField.h:
@protocol MyTextFieldDelegate <NSObject>
@optional
- (void)textFieldDidDelete;
@end
@interface MyTextField : UITextField<UIKeyInput>
//create "myDelegate"
@property (nonatomic, assign) id<MyTextFieldDelegate> myDelegate;
@end
My TextField.m: 我的TextField.m:
- (void)deleteBackward {
[super deleteBackward];
if ([_myDelegate respondsToSelector:@selector(textFieldDidDelete)]){
[_myDelegate textFieldDidDelete];
}
}
In my ViewController that I would like to access the UITextField subclass I do the following: 在我想访问UITextField子类的ViewController中,执行以下操作:
#import "MyTextField.h"
<MyTextFieldDelegate>
@property (nonatomic, strong) IBOutlet MyTextField *passcode1;
self.passcode1.myDelegate = self;
Any ideas as to why I am getting the unrecognized selector error? 关于为什么我收到无法识别的选择器错误的任何想法? It looks to me like I have done everything correctly in subclassing UITextField. 在我看来,我在子类化UITextField中正确地完成了所有操作。
The problem is with your outlet. 问题出在您的插座上。 The property is defined with your custom class but in a Interface Builder you added a plain old UITextField. 该属性是使用您的自定义类定义的,但是在Interface Builder中,您添加了一个普通的旧UITextField。 Hence the error at runtime. 因此在运行时出现错误。 In Interface Buldier, update the text field's class to be your custom class. 在Interface Buldier中,将文本字段的类更新为您的自定义类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.