iOS TextField 监听实时输入 避免删除键失灵 监听 删除键点击

如果有帮助到你就给个赞吧 谢谢咯...

// 添加监听方法

[_textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

// MARK:  =========  textfield 监听实现  =========

-(void)textFieldDidChange :(UITextField *)theTextField{

if (theTextField.text.length != 11) {  //  11位数字

_loginButton.userInteractionEnabled = NO;

[_loginButton setBackgroundColor:kRGBColor(209, 209, 209, 1)];

} else {

_loginButton.userInteractionEnabled = YES;

[_loginButton setBackgroundColor:kBackgroundColor];

}

}```

   UITextFieldDelegate  代理方法  避免删除键失灵

  • (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

if (range.length == 1 && string.length == 0) {

return YES;

} else if ([textField isEqual:self.accountTF]) {

return textField.text.length < 11;

}

return YES;

}


监听删除键的点击要添加textField分类

.h


//// Created by SDL on 6/20/16.// Copyright © 2016 SDL. All rights reserved.

import "UITextField+Monitor_TextField.h"

@protocol WsTextFieldDelegate
@optional

  • (void)textFieldDidDeleteBackward:(UITextField *)textField;@end@interface UITextField (Monitor_TextField)
    @property (weak, nonatomic) iddelegate;

@end

/**

  • 监听删除按钮

  • object:UITextField

*/

extern NSString * const WsTextFieldDidDeleteBackwardNotification;


.m

//
// Created by SDL on 6/20/16.
// Copyright © 2016 SDL. All rights reserved.
//

import "UITextField+Monitor_TextField.h"

import <objc/objc-runtime.h>

NSString * const WsTextFieldDidDeleteBackwardNotification = @"com.whojun.textfield.did.notification";

@implementation UITextField (Monitor_TextField)

  • (void)load {
    //交换2个方法中的IMP
    Method method_First = class_getInstanceMethod([self class], NSSelectorFromString(@"deleteBackward"));
    Method method_Second = class_getInstanceMethod([self class], @selector(wj_deleteBackward));
    method_exchangeImplementations(method_First, method_Second);
    }
  • (void)wj_deleteBackward {
    [self wj_deleteBackward];

    if ([self.delegate respondsToSelector:@selector(textFieldDidDeleteBackward:)])
    {
    id <Ws_TextFieldDelegate> delegate = (id<Ws_TextFieldDelegate>)self.delegate;
    [delegate textFieldDidDeleteBackward:self];
    }

    [[NSNotificationCenter defaultCenter] postNotificationName:WsTextFieldDidDeleteBackwardNotification object:self];
    }

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,260评论 25 709
  • 1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现cl...
    以德扶人阅读 7,421评论 2 50
  • 第一课 悬灸适合大众,明灸更重气场所以要练功打坐站桩。什么是明灸呢?就是把艾绒放在人的某个穴位上或病灶处灸,解除病...
    菡丹阅读 6,457评论 0 1
  • 想给你写一首没有旋律的歌 让你随时能够吟唱 你快乐时 歌是快乐的 你不快乐时 我是你的 我会以我之力 让你快乐 因...
    你这样一点都不酷阅读 1,716评论 0 2
  • 本文根据众多互联网博客内容整理后形成,引用内容的版权归原始作者所有,仅限于学习研究使用,不得用于任何商业用途。 用...
    深红的眼眸阅读 5,265评论 0 0