监听UITextField键盘删除按钮事件
2016-02-24 16:57
459 查看
//
// UITagTextField.h
//
// Created by Peace on 6/10/15.
//
#import <UIKit/UIKit.h>
@protocol UITagTextFieldDelegate;
@interface UITagTextField :
UITextField <UIKeyInput>
@property (nonatomic,
assign) id<UITagTextFieldDelegate> tagDelegate;
@property (nonatomic,
assign) BOOL isWillDelete;
@end
@protocol UITagTextFieldDelegate <NSObject>
@optional
- (void)textFieldDidDelete:(UITagTextField *)textField;
@end
//
// UITagTextField.m
//
// Created by Peace on 6/10/15.
//
#import "UITagTextField.h"
@implementation UITagTextField
- (void)deleteBackward
{
[super
deleteBackward];
if (!self.isWillDelete) {
if ([_tagDelegate
respondsToSelector:@selector(textFieldDidDelete:)]){
[_tagDelegate
textFieldDidDelete:self];
}
}
}
- (BOOL)keyboardInputShouldDelete:(UITextField *)textField
{
BOOL shouldDelete =
YES;
self.isWillDelete =
YES;
if ([UITextField
instancesRespondToSelector:_cmd]) {
BOOL (*keyboardInputShouldDelete)(id,
SEL, UITextField *) = (BOOL (*)(id,
SEL, UITextField *))[UITextField
instanceMethodForSelector:_cmd];
if (keyboardInputShouldDelete) {
shouldDelete = keyboardInputShouldDelete(self,
_cmd, textField);
}
}
if (![textField.text
length] && [[[UIDevice
currentDevice] systemVersion]
intValue] >= 8) {
//[self deleteBackward];
if ([_tagDelegate
respondsToSelector:@selector(textFieldDidDelete:)]){
[_tagDelegate
textFieldDidDelete:self];
}
}
return shouldDelete;
}
@end
// UITagTextField.h
//
// Created by Peace on 6/10/15.
//
#import <UIKit/UIKit.h>
@protocol UITagTextFieldDelegate;
@interface UITagTextField :
UITextField <UIKeyInput>
@property (nonatomic,
assign) id<UITagTextFieldDelegate> tagDelegate;
@property (nonatomic,
assign) BOOL isWillDelete;
@end
@protocol UITagTextFieldDelegate <NSObject>
@optional
- (void)textFieldDidDelete:(UITagTextField *)textField;
@end
//
// UITagTextField.m
//
// Created by Peace on 6/10/15.
//
#import "UITagTextField.h"
@implementation UITagTextField
- (void)deleteBackward
{
[super
deleteBackward];
if (!self.isWillDelete) {
if ([_tagDelegate
respondsToSelector:@selector(textFieldDidDelete:)]){
[_tagDelegate
textFieldDidDelete:self];
}
}
}
- (BOOL)keyboardInputShouldDelete:(UITextField *)textField
{
BOOL shouldDelete =
YES;
self.isWillDelete =
YES;
if ([UITextField
instancesRespondToSelector:_cmd]) {
BOOL (*keyboardInputShouldDelete)(id,
SEL, UITextField *) = (BOOL (*)(id,
SEL, UITextField *))[UITextField
instanceMethodForSelector:_cmd];
if (keyboardInputShouldDelete) {
shouldDelete = keyboardInputShouldDelete(self,
_cmd, textField);
}
}
if (![textField.text
length] && [[[UIDevice
currentDevice] systemVersion]
intValue] >= 8) {
//[self deleteBackward];
if ([_tagDelegate
respondsToSelector:@selector(textFieldDidDelete:)]){
[_tagDelegate
textFieldDidDelete:self];
}
}
return shouldDelete;
}
@end
相关文章推荐
- Swift社交应用文本输入优化汇总
- UITextField常用delegate
- UITextField
- iOS开发1:UITextField控件属性
- [Swift]UIKit学习之UITextField的用法
- IOS开发Swift语言UITextField设置靠左的间距
- IOS开发Swift语言键盘遮挡输入框(UITextField)
- iOS UITextField相关
- cocos2d-x 一些有用的函数
- UITextField的使用
- UITextField输入框的字靠右一点显示
- UITextView光标位置的设置
- UISearchbar设置placeholder字体颜色大小
- 第一个IOS程序(代码版)
- iOS UI三大控件和应用程序启动流程
- 修改UITextField的placeholder颜色
- iOS-TextField知多少 (1)
- iOS-TextField知多少 (2)
- 微信聊天
- IOS Label 同步 UITextfield 显示输入内容(删除)