我设置了 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] 删除。
我来说两句