如果有幫助到你就給個贊吧 謝謝咯...
// 添加監(jiān)聽方法
[_textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
// MARK: ========= textfield 監(jiān)聽實現(xiàn) =========
-(void)textFieldDidChange :(UITextField *)theTextField{
if (theTextField.text.length != 11) { // 11位數(shù)字
_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;
}
監(jiān)聽刪除鍵的點擊要添加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
/**
監(jiān)聽刪除按鈕
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