iOS之UITextField切换焦点
2016-04-07 11:16
357 查看
如图:页面中有两个密码输入框,一个UIButton,初始化时UIButton不可点击,当两个输入框内容均不为空,按钮可以点击;若任意一个为空,则不可点击。
很多时候,我们都会重写UITextField的代理方法,通过输入框内容变化来动态决定UIButton是否可用。
但经过测试发现:两个UITextField输入内容后,在键盘不关闭的情况下把光标切换至另外一个输入框,一旦用退格键删除最后一个字符或中间某个字符,整个UITextfield的内容被清空了,这时Button不会起任何变化。
很多时候,我们都会重写UITextField的代理方法,通过输入框内容变化来动态决定UIButton是否可用。
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { ....处理你的Button....... return YES; }
但经过测试发现:两个UITextField输入内容后,在键盘不关闭的情况下把光标切换至另外一个输入框,一旦用退格键删除最后一个字符或中间某个字符,整个UITextfield的内容被清空了,这时Button不会起任何变化。
解决方法:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextDidChange:) name:UITextFieldTextDidChangeNotification object:nil ];
#pragma mark - UITextField Delegate - (void)textFieldTextDidChange:(NSNotification *)notification { UITextField *tf = (UITextField *)[self.view viewWithTag:TextField_Tag_Password]; UITextField *tf2 = (UITextField *)[self.view viewWithTag:TextField_Tag_Password2]; UIButton *btn = (UIButton *)[self.view viewWithTag:Button_Tag_Confirm]; if ([Utils isEmpty:tf.text] || [Utils isEmpty:tf2.text]) { btn.backgroundColor = DisableButtonColor; btn.enabled = NO; } else { btn.backgroundColor = ThemeColor; btn.enabled = YES; } }
相关文章推荐
- jd-gui安装
- Android初学习 - android:sharedUserId="android.uid.system" 应用程序获得系统权限
- UITableView 基本使用方法总结
- Cadence中Quickview中没有预览
- NGUI实现滑动翻页效果实例代码
- JMS使用ActiveMQ实现Queue和Topic两种模式
- SUID浅析
- UIWindow输入密码
- 52. N-Queens II
- [IOS]为UILabel添加点击事件
- scrollview和UItableviewController结合使用时 要求穿透 NavigationBar和TabBar的技巧
- 建造者模式Builder Pattern
- GNUI30天从入门到吐血
- Buiding in Sandbox--2016微软预科生技术岗笔试题四--Java
- Qt Quick 简介
- 3.8.2 smtpd_chat_query和smtpd_chat_replay:与smtp客户端交互
- [na][tools]快速ping网段工具-QuickPing
- [转]MyBatis传入多个参数的问题 - mingyue1818
- [转]MyBatis传入多个参数的问题 - mingyue1818
- UIViewContentMode各类型效果