如果有帮助到你就给个赞吧 谢谢咯...
// 添加监听方法
[_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
网友评论