Swift If 语句中的错误

Bug in Swift If statement

我设置了 4 个 UITextFields 和 1 个 UIButton。如果用户提供用户名、电子邮件、密码等数据,这将用作注册页面。因此,只有在所有 UI 文本字段都不为空的情况下,注册按钮才会启用,以便用户可以点击内部并转到下一个用户界面视图。一切正常,但我注意到一个小错误,它进入了我最后的神经 XD。

如果用户用所有需要的信息填充所有 UItextFields 但由于某种原因退格到其中一个直到该字段为空,然后点击注册按钮,即使有一个空字段,注册也会成功.拜托,我已经花了将近一个星期的时间来解决这个问题。

我设置UIbotton的方式:

private let registerButton : UIButton = {
    let button = UIButton(type: .system)
    button.translatesAutoresizingMaskIntoConstraints = false
    button.setTitle("Registrar", for: .normal)
    button.backgroundColor = UIColor.blue
    button.layer.cornerRadius = 5
    button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
    button.setTitleColor(.black, for: .normal)
    button.addTarget(self, action: #selector(handleSignUp), for: .touchUpInside)
    button.isEnabled = true
    return button
}()

负责验证所有字段是否已被用户填写的块代码。

@objc private func handleSignUp () {
    let userName = userNameTexField.text
    let email = emailTextField.text
    let password = passwordTextField.text
    let confirmPassword = confirmPasswordField.text
    let birthDate = birthDateTextField.text

    if userName?.isEmpty ?? true && email?.isEmpty ?? true && password?.isEmpty ?? true && confirmPassword?.isEmpty ?? true && birthDate?.isEmpty ?? true {
        let alert = UIAlertController(title: nil, message: "Youmust fill all the fields with all the required infotmation to signup", preferredStyle: .alert)
        let okAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
        alert.addAction(okAction)
        present(alert, animated: true, completion: nil)
    }else {
        emailTextField.isUserInteractionEnabled = false
        passwordTextField.isUserInteractionEnabled = false
        confirmPasswordField.isUserInteractionEnabled = false
        birthDateTextField.isUserInteractionEnabled = false
        performSegue(withIdentifier: "goToHome", sender: nil)
        print("LogIn succesful!")
        print (userAge)
    }
}

我希望修复此错误,因此当用户出于某种原因删除其中一个字段时,警报会再次弹出,告诉用户填写所有内容以进行注册。

您应该在 if 中使用 || 而不是 &&,因为您希望在至少有一个字段为空时显示警报

您要求所有字段都为空才能触发警报。您需要知道其中一个是空的。尝试将 && 更改为 ||

if userName?.isEmpty ?? true || email?.isEmpty ?? true || password?.isEmpty ?? true || confirmPassword?.isEmpty ?? true || birthDate?.isEmpty ?? true

您需要使用运算符 or 而不是 and,因为您检查了任何空文本字段:

@objc private func handleSignUp () {

    if textField1.text!.isEmpty || textField2.text!.isEmpty || textField3.text!.isEmpty || textField4.text!.isEmpty || textField5.text!.isEmpty {
        let alert = UIAlertController(title: nil, message: "Youmust fill all the fields with all the required infotmation to signup", preferredStyle: .alert)
        let okAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
        alert.addAction(okAction)
        present(alert, animated: true, completion: nil)
    }else {
        textField2.isUserInteractionEnabled = false
        textField3.isUserInteractionEnabled = false
        textField4.isUserInteractionEnabled = false
        textField5.isUserInteractionEnabled = false
        performSegue(withIdentifier: "goToHome", sender: nil)
        print("LogIn succesful!")
        print ("userAge")

    }
}