UITextField 事件監(jiān)聽四種方式

一、添加監(jiān)聽對象

-(void)awakeFromNib{
    [super awakeFromNib];
    [self addTarget:self action:@selector(textFildBegin) forControlEvents:UIControlEventEditingDidBegin];
    [self addTarget:self action:@selector(textFildEnd) forControlEvents:UIControlEventEditingDidEnd];
    
    
    
}
-(void)textFildBegin{
    NSLog(@"begin");

}

-(void)textFildEnd{
    NSLog(@"end");
}


二、代理

@interface ViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet XMGLoginTextField *phoneNumber;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.phoneNumber.delegate = self;
    // Do any additional setup after loading the view, typically from a nib.
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark -UITextFieldDelegate
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    NSLog(@"begin");
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    NSLog(@"end");
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [self.view endEditing:YES];
}

三、通知

- (void)awakeFromNib
{
    // 設(shè)置光標(biāo)顏色
    self.tintColor = [UIColor whiteColor];
    // 設(shè)置默認(rèn)的占位文字顏色
    [self setValue:[UIColor grayColor] forKeyPath:XMGPlaceholderColorKey];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(beginEditing) name:UITextFieldTextDidBeginEditingNotification object:self];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endEditing) name:UITextFieldTextDidEndEditingNotification object:self];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)beginEditing
{
    [self setValue:[UIColor whiteColor] forKeyPath:XMGPlaceholderColorKey];
}

- (void)endEditing
{
    [self setValue:[UIColor grayColor] forKeyPath:XMGPlaceholderColorKey];
}
@end

四、是否是鍵盤的第一響應(yīng)用者

//鍵盤出現(xiàn)
-(BOOL)becomeFirstResponder{
        self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    return [super becomeFirstResponder];
}

//鍵盤退出
-(BOOL)resignFirstResponder{
    self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder attributes:@{NSForegroundColorAttributeName:[UIColor darkGrayColor]}];
    return [super resignFirstResponder];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,556評論 25 708
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,255評論 4 61
  • 你,是我心空的一顆星辰 閃閃爍爍,飄飄忽忽 我,多想把你定格在眼前 夜夜年年,永永遠(yuǎn)遠(yuǎn) 夜晚是時間的傷痕 悄然劃滿...
    陳篤閱讀 195評論 0 2
  • 只一眼便賠上了一生……
    九月天蝎閱讀 329評論 0 0
  • 第081講 米奇老鼠的陰謀詭計 年金的概念 持續(xù)多期的收入稱為“年金”。 今年我賺一塊錢,明天我賺一塊錢,后年我賺...
    勢從千里奔閱讀 1,582評論 0 0