它会一直显示警报“密码不匹配”,我看不出问题出在哪里。 而且,如果我删除与密码匹配的代码,则会崩溃。 我肯定知道IBOutlets和IBAction已正确连接。
@IBAction func signupButton(_ sender: UIButton) {
let userEmail = email
let userPassword = password
let userRepeatPassword = repeatPassword
// Check empty fields
if (email.hasText == false) || (password.hasText == false) || (repeatPassword.hasText == false) {
alertMessage(userMessage: "All fields are required!")
return
}
// Check if passwords match
if repeatPassword != password {
alertMessage(userMessage: "Passwords do not match!")
return
}
// Store data
UserDefaults.standard.set(userEmail, forKey: "userEmail")
UserDefaults.standard.set(userPassword, forKey: "userPassword")
UserDefaults.standard.synchronize()
// Display alert message with confirmation
var alert = UIAlertController(title: "Alert", message: "Signed up!", preferredStyle: UIAlertController.Style.alert)
let okAction = UIAlertAction(title: "Ok", style: UIAlertAction.Style.default) { action in
self.dismiss(animated: true, completion: nil)
}
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
}
func alertMessage (userMessage: String) {
var alert = UIAlertController(title: "Alert", message: userMessage, preferredStyle: UIAlertController.Style.alert)
let okAction = UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: nil)
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
}
}
答案 0 :(得分:2)
我怀疑这些行是错误的:
let userEmail = email
let userPassword = password
let userRepeatPassword = repeatPassword
考虑到您的其他代码,我怀疑email
和其他是UITextFields,而不是String。我怀疑你是这个意思:
let userEmail = email.text ?? ""
let userPassword = password.text ?? ""
let userRepeatPassword = repeatPassword.text ?? ""