OGeek|极客世界-中国程序员成长平台

标题: ios - 跟踪多个 UITextField 的文本长度 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 09:56
标题: ios - 跟踪多个 UITextField 的文本长度

我想跟踪 2 个 UITextField,如果它们为空,则禁用完成按钮。在 viewDidLoad 我禁用了按钮,因为所有 UITextField 都是空的。所有文本字段都有委托(delegate),我在 -(BOOL)textFieldUITextField *)textField shouldChangeCharactersInRangeNSRange)range replacementStringNSString *)string

中跟踪它们的状态
if (textField == self.emailTextField) {
        if (self.passwordTextField.hasText && [string length] > 0) {
            [self.loginButton setEnabled:YES];
        }
        else {
            [self.loginButton setEnabled:NO];
        }
    }
    else if (textField == self.passwordTextField) {
        if (self.emailTextField.hasText && [string length] > 0) {
            [self.loginButton setEnabled:YES];
        }
        else {
            [self.loginButton setEnabled:NO];
        }
    }

    return YES;

我知道这不是跟踪的最佳解决方案,因为现在当有人只删除字符、字符串为空且按钮被禁用时,我遇到了问题。提前感谢您与我一起寻找最佳解决方案。



Best Answer-推荐答案


您不需要委托(delegate)。您可以为文本字段连接 IBAction“编辑确实更改”.. 在其中您可以这样做

- (IBAction)textFieldChangedUITextField *)sender {
    [self.loginButton setEnabledself.passwordTextField.hasText && self.emailTextField.hasText)];
}

关于ios - 跟踪多个 UITextField 的文本长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33953925/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4