UITextView监听删除键:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqualToString:@""]) {
/**< 在这里处理删除键的逻辑 */
}
}
UITextField监听删除键:
UITextField其实里面用的是UIKeyInput,UIKeyInput有一个系统自带的删除方法:
protocol UIKeyInput <UITextInputTraits>
- (BOOL)hasText;
- (void)insertText:(NSString *)text;
- (void)deleteBackward; <-------就是它删除方法
@end
自定义一个控件继承 UITextField:
.h文件
#import <UIKit/UIKit.h>
@class YNTextField;
@protocol YNTextFieldDelegate <NSObject>
- (void)ynTextFieldDeleteBackward:(YNTextField *)textField;
@end
@interface YNTextField : UITextField
@property (nonatomic, assign) id <YNTextFieldDelegate> yn_delegate;
@end
.m文件
#import "YNTextField.h"
@implementation YNTextField
- (void)deleteBackward {
!!!这里要调用super方法,要不然删不了东西
[super deleteBackward];
if ([self.yn_delegate respondsToSelector:@selector(ynTextFieldDeleteBackward:)]) {
[self.yn_delegate ynTextFieldDeleteBackward:self];
}
}
@end
这样就可以监听UITextField的删除键了
网友评论