您的位置:首页 > 产品设计 > UI/UE

监听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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息