UITextFieldDelegate協議,uitextfielddelegate
很多人都認為UITextField很簡單,為什麼會寫這個協議呢?
因為在實際開發中可能會用到;
比如要做到的效果:
文字框下面的底線的顏色要隨著輸入的狀態變化;
點擊對應的文字框,其下面的線條變為綠色,結束編輯,變為灰色;
要熟悉UITextFieldDelegate協議才能做到這一點;
首先,先看一下官方的說明:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing.- (void)textFieldDidBeginEditing:(UITextField *)textField; // became first responder- (BOOL)textFieldShouldEndEditing:(UITextField *)textField; // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end- (void)textFieldDidEndEditing:(UITextField *)textField; // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text- (BOOL)textFieldShouldClear:(UITextField *)textField; // called when clear button pressed. return NO to ignore (no notifications)- (BOOL)textFieldShouldReturn:(UITextField *)textField; // called when 'return' key pressed. return NO to ignore.
1、
- (void)textFieldDidBeginEditing:(UITextField *)textField;
文字框已經開始編輯
2、
- (void)textFieldDidEndEditing:(UITextField *)textField;
文字框已經結束編輯
3、其他設定是否允許的輸入控制
1、- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing.
//控制是否允許輸入,傳回值是NO的時候,禁止輸入;
2、- (BOOL)textFieldShouldEndEditing:(UITextField *)textField; // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end //是否結束輸入,傳回值是YES時候,允許停止輸入,並且釋放第一響應;返回NO的時候,則相反;3、- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text
//是否改變輸入文本,返回NO的時候,不儲存修改
4、- (BOOL)textFieldShouldClear:(UITextField *)textField; // called when clear button pressed. return NO to ignore (no notifications)
//當清空按鈕時候調用,返回是NO的時候,忽視清空
5、- (BOOL)textFieldShouldReturn:(UITextField *)textField; // called when 'return' key pressed. return NO to ignore.
//當點擊return按鈕時候調用該方法,返回是NO的時候,不做任何響應;