UITextView監(jiān)聽(tīng)刪除鍵:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqualToString:@""]) {
/**< 在這里處理刪除鍵的邏輯 */
}
}
UITextField監(jiān)聽(tīng)刪除鍵:
UITextField其實(shí)里面用的是UIKeyInput,UIKeyInput有一個(gè)系統(tǒng)自帶的刪除方法:
protocol UIKeyInput <UITextInputTraits>
- (BOOL)hasText;
- (void)insertText:(NSString *)text;
- (void)deleteBackward; <-------就是它刪除方法
@end
自定義一個(gè)控件繼承 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 {
!R路汀!這里要調(diào)用super方法壤靶,要不然刪不了東西
[super deleteBackward];
if ([self.yn_delegate respondsToSelector:@selector(ynTextFieldDeleteBackward:)]) {
[self.yn_delegate ynTextFieldDeleteBackward:self];
}
}
@end
這樣就可以監(jiān)聽(tīng)UITextField的刪除鍵了