我想在我的app中开发类似功能的密码。因为我正在使用textfields.Entering passocode成功完成。现在我想实现在numberpad的后退按钮操作上输入的清晰密码文本。 这是我声明协议的代码片段
@objc protocol BackPressDelegate {
func backPressed(info: NSDictionary)
}
class CustomTextField: UITextField, UIKeyInput {
var del1:BackPressDelegate?
override func deleteBackward() {
super.deleteBackward()
if ((self.delegate?.respondsToSelector("backPressed")) != nil) {
self.del1?.backPressed(["tag":self.tag])
}
}
}
为此,我创建了uitextfield的子类 覆盖' deleteBackward()' method.i也设置了委托 故事板中的uitextfield。我的问题是deleteBackward是 当我按下后退按钮时,由于某种原因没有被调用 数字小。
答案 0 :(得分:2)
试试这个子类:
// customTextField.h
#import <UIKit/UIKit.h>
@protocol customTextFieldDelegate <NSObject>
@optional
- (void)TextFieldWillDelete:(TextField*)textField;
@end
// customTextField.m
#import "customTextField.h"
@implementation customTextField
- (void)deleteBackward {
if([[[UIDevice currentDevice] systemVersion] intValue] < 8) {
[super deleteBackward];
}
if ([_deleteDelegate respondsToSelector:@selector(TextFieldWillDelete:)]){
[_deleteDelegate TextFieldWillDelete:self];
}
}
- (BOOL)keyboardInputShouldDelete:(TextField *)textField {
BOOL shouldDelete = YES;
if ([TextField instancesRespondToSelector:_cmd]) {
BOOL (*keyboardInputShouldDelete)(id, SEL, UITextField *) = (BOOL (*)(id, SEL, UITextField *))[UITextField instanceMethodForSelector:_cmd];
if (keyboardInputShouldDelete) {
shouldDelete = keyboardInputShouldDelete(self, _cmd, textField);
}
}
if ([[[UIDevice currentDevice] systemVersion] intValue] >= 8) {
[self deleteBackward];
}
return shouldDelete;
}
// yourViewController.m
@interface yourViewController () <customTextFieldDelegate>
@end
@implementation
-(void)TextFieldWillDelete:(TextField*)textField {
NSLog(@"will delete: %@", textField.text);
}
@end
与UITextView
相同。