Reputation: 3519
The clear answer to detecting a backspace is by creating a subclass of UITextField and overriding the deleteBackward
property.
I've created a subclass a UITextField subclass, but am getting this error:
'NSInvalidArgumentException', reason: '-[UITextField setMyDelegate:]: unrecognized selector sent to instance
This is my subclass code:
My 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:
- (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:
#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.
Upvotes: 2
Views: 470
Reputation: 318794
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. Hence the error at runtime. In Interface Buldier, update the text field's class to be your custom class.
Upvotes: 3