Swift If 语句中的错误

埃罗伊

我设置了 4 个 UITextFields 和一个 UIButton。如果用户提供用户名、电子邮件、密码等数据,这将用作注册页面。因此,只有在所有 UItextfield 都不为空的情况下,才会启用注册按钮,以便用户可以点击内部并转到下一个界面视图。一切正常,但我注意到一个小错误,它正在进入我最后的神经 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 userName?.isEmpty ?? true || email?.isEmpty ?? true || password?.isEmpty ?? true || confirmPassword?.isEmpty ?? true || birthDate?.isEmpty ?? true

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在Swift switch语句中使用`let`编译错误

来自分类Dev

Swift:IF 语句中的多个条件

来自分类Dev

Apple的Swift文档中有关在if语句中强制展开的错误?

来自分类Dev

在Swift中的if语句中使用let

来自分类Dev

Swift:if let语句中的条件类型转换(as?,as!)

来自分类Dev

Swift中Guard语句中的多个条件

来自分类Dev

在if语句中定义'Cell'的实例-Swift

来自分类Dev

在Swift的If语句中设置@State var

来自分类Dev

在Swift条件语句中使用Bool会产生错误:“从未使用过变量newCard的初始化”

来自分类Dev

我使用 swift 收到错误“字符串”无法匹配我的 Switch 语句中的值,为什么?

来自分类Dev

Switch语句中的错误

来自分类Dev

if语句中的错误

来自分类Dev

if else语句中的错误

来自分类Dev

LINQ语句中的错误

来自分类Dev

cmake IF 语句中的错误

来自分类Dev

Swift:错误忽略了 try 语句

来自分类Dev

Swift-在switch语句中使用枚举

来自分类Dev

Swift:switch语句中未解析的标识符

来自分类Dev

在if语句中使用多个包含功能(Swift)

来自分类Dev

在iOS中使用Swift消除switch语句中的重复

来自分类Dev

Swift switch 语句中的“as”测试始终是真正的警告

来自分类Dev

iOS swift 3 如何更改 switch 语句中的类型

来自分类Dev

Swift Direction Request 无法在 if 语句中正确计算

来自分类Dev

If-Then语句中的Python错误

来自分类Dev

在if语句中出现错误

来自分类Dev

if语句中缺少值错误

来自分类Dev

SUBSEQ语句中的错误答案

来自分类Dev

更新语句中的错误

来自分类Dev

if语句中的Python缩进错误