UITextFieldDelegate协议

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

很多人都认为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 editingsessionto 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 buttonPRessed. 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的时候,不做任何响应;

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航