您的位置:首页 > 产品设计 > UI/UE

iOS之UITextField切换焦点

2016-04-07 11:16 357 查看
如图:页面中有两个密码输入框,一个UIButton,初始化时UIButton不可点击,当两个输入框内容均不为空,按钮可以点击;若任意一个为空,则不可点击。



很多时候,我们都会重写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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: