關(guān)鍵知識點:如何監(jiān)聽一個控件內(nèi)部的事件(四種)
- 第一種:如果繼承自UIControl
- (void)addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
- 第二種:代理
- 第三種:通知
- 第四種:利用內(nèi)部的某些機(jī)制
比如重寫UITextField的
becomeFirstResponder和resignFirstResponder來監(jiān)聽UITextField的獲得焦點和失去焦點事件
例子如下:
在登錄注冊模塊一般會用到
效果圖:

2.png
** 需求:**
- 一般光標(biāo)顏色是藍(lán)色,想要在聚焦時,光標(biāo)的顏色進(jìn)行改變;
- 聚焦時彈出鍵盤,聚焦失效時,鍵盤縮回。
** 解決:**
- 一般通過自定義
UITextField來實現(xiàn),以下設(shè)置都是在自定義的UITextField實現(xiàn)。 - 自定義完成后,要在Xib中需要設(shè)置的
UITextField寫入類名。如下:

11.png
一、修改光標(biāo)顏色
方法一: 直接設(shè)置
這個需要對修改光標(biāo)顏色的每一個UITextField進(jìn)行設(shè)置。

3.png
方法二:代碼
這個需要在自定義的UITextField中設(shè)置一次即可。
@property (weak, nonatomic) IBOutlet UITextField *SecretLabel;
self.SecretLabel.tintColor = [UIColor whiteColor];
二、光標(biāo)聚焦
方法一:推薦使用,監(jiān)聽文字的編輯
- addTarget
[textField addTarget:target action:@selector(editingDidBegin) forControlEvents:UIControlEventEditingDidBegin];
[textField addTarget:target action:@selector(editingDidEnd) forControlEvents:UIControlEventEditingDidEnd];
文字編輯的三種狀態(tài):editingDidBegin、editingDidEnd、editingChange
UIControlEventEditingDidBegin
- 開始編輯
- 獲得焦點
- 彈出鍵盤
UIControlEventEditingDidEnd
- 結(jié)束編輯
- 失去焦點
- 退下鍵盤
代碼如下:
static NSString *const ZSPlaceholderColorKey = @"placeholderLabel.textColor";
@implementation ZBSLoginRegisterTextField
-(void)awakeFromNib
{
// 光標(biāo)的顏色
self.tintColor = [UIColor whiteColor];
// 設(shè)置輸入的文字顏色
self.textColor = [UIColor whiteColor];
// 默認(rèn)占位文字為灰色
[self setValue:[UIColor grayColor] forKeyPath:ZSPlaceholderColorKey];
[self addTarget:self action:@selector(editingDidBegin) forControlEvents:UIControlEventEditingDidBegin];
[self addTarget:self action:@selector(editingDidEnd) forControlEvents:UIControlEventEditingDidEnd];
}
/**
* 開始編輯
*/
- (void)editingDidBegin
{
[self setValue:[UIColor whiteColor] forKeyPath:ZSPlaceholderColorKey];
}
/**
* 結(jié)束編輯
*/
- (void)editingDidEnd
{
XMGLogFunc
[self setValue:[UIColor grayColor] forKeyPath:ZSPlaceholderColorKey];
}
拓展:
// 監(jiān)聽文字改變
[self addTarget:self action:@selector(editingChange) forControlEvents:UIControlEventEditingChanged];
方法二、代理
- 遵循協(xié)議
<UITextFieldDelegate> - 設(shè)置代理:
self.delegate = self; - 實現(xiàn)代理方法
#pragma mark - <UITextFieldDelegate>
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self setValue:[UIColor whiteColor] forKeyPath:XMGPlaceholderColorKey];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self setValue:[UIColor grayColor] forKeyPath:XMGPlaceholderColorKey];
}
方法三、通知
一定要記得移除監(jiān)聽器
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(beginEditing) name:UITextFieldTextDidBeginEditingNotification object:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endEditing) name:UITextFieldTextDidEndEditingNotification object:self];
/**
* 移除監(jiān)聽器
*/
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)beginEditing
{
[self setValue:[UIColor whiteColor] forKeyPath:XMGPlaceholderColorKey];
}
- (void)endEditing
{
[self setValue:[UIColor grayColor] forKeyPath:XMGPlaceholderColorKey];
}
方法四:基于對UITextField非常熟悉,利用內(nèi)部的某些機(jī)制(成為第一響應(yīng)者)
/**
* 文本框聚焦時調(diào)用(彈出當(dāng)前文本框?qū)?yīng)的鍵盤時調(diào)用)
*/
- (BOOL)becomeFirstResponder
{
self.placeholderColor = XMGPlaceholderFocusColor;
return [super becomeFirstResponder];
}
/**
* 文本框失去焦點時調(diào)用(隱藏當(dāng)前文本框?qū)?yīng)的鍵盤時調(diào)用)
*/
- (BOOL)resignFirstResponder
{
self.placeholderColor = XMGPlaceholderDefaultColor;
return [super resignFirstResponder];
}