监听键盘的删除方法

来源:互联网 发布:python tf idf 的代码 编辑:程序博客网 时间:2024/05/17 00:05

查看API我们不难发现,其实系统是有事件抛出来的:

@protocol UIKeyInput <UITextInputTraits>

- (BOOL)hasText;
- (void)insertText:(NSString *)text;
- (void)deleteBackward;

@end

    NS_CLASS_AVAILABLE_IOS(2_0) @interface UITextField : UIControl <UITextInput, NSCoding> 

所以,只需要重写即可


自定义一个类,继承UITextField 
#import <UIKit/UIKit.h>
@class  JYTextView;
@protocol JYTextViewDelegate<NSObject>
-(void)jyTextViewDeleteBackward:(JYTextView*)textview;
@end
@interface JYTextView : UITextView
@property(nonatomic,weak) id<JYTextViewDelegate>jy_Delegate;
@end


#import "JYTextView.h"

@implementation JYTextView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(void)deleteBackward
{
    [super deleteBackward];
    if ([self.jy_Delegate respondsToSelector:@selector(jyTextViewDeleteBackward:)]) {
        [self.jy_Delegate jyTextViewDeleteBackward:self];
    }
}
@end


原创粉丝点击