Ios 如何关闭弹出的错误消息
我正在创建一个应用程序,当用户将文本字段留空或两个密码不匹配时,将显示错误警报 问题是,当信息正确且两个密码匹配时,仍会显示错误警报 代码如下所示:Ios 如何关闭弹出的错误消息,ios,swift,Ios,Swift,我正在创建一个应用程序,当用户将文本字段留空或两个密码不匹配时,将显示错误警报 问题是,当信息正确且两个密码匹配时,仍会显示错误警报 代码如下所示: //creating an action that will check when the sign up button is selected. @IBAction func registerButtonTapped(_ sender: Any) { let userEmail = userEmailTextField.text;
//creating an action that will check when the sign up button is selected.
@IBAction func registerButtonTapped(_ sender: Any) {
let userEmail = userEmailTextField.text;
let userPassword = userPasswordTextField.text;
let userRepeatPassword = repeatPasswordTextField.text;
// if one of the fields is empty the user must repeat the password
if ((userEmail?.isEmpty)! || (userPassword?.isEmpty)! || (userRepeatPassword != nil)) {
//this is the alert pop up shown to the user.
let alertController = UIAlertController(
title: "Error!!!!!!",
message: "Something Went Wrong, Try Again",
preferredStyle: UIAlertControllerStyle.alert
)
let cancelAction = UIAlertAction(
title: "Cancel",
style: UIAlertActionStyle.destructive) { (action) in
}
let confirmAction = UIAlertAction(
title: "OK", style: UIAlertActionStyle.default) { (action) in
}
alertController.addAction(confirmAction)
alertController.addAction(cancelAction)
self.present(alertController, animated: true){ () -> Void in
return;
}
}
func registerButtonTapped(_ sender: Any) {
//check if password and repeat password are the same
if (userPassword != userRepeatPassword) {
//display an alert message, user will not be able to continue // not too sure if this works
let alertController = UIAlertController(
title: "Error!!!!!!",
message: "Something Went Wrong Try Again",
preferredStyle: UIAlertControllerStyle.alert
)
let cancelAction = UIAlertAction(
title: "Cancel",
style: UIAlertActionStyle.destructive) { (action) in
}
let confirmAction = UIAlertAction(
title: "OK",
style: UIAlertActionStyle.default) { (action) in
}
alertController.addAction(confirmAction)
alertController.addAction(cancelAction)
self.present(alertController, animated: true){ () -> Void in
return;
}
输入正确的信息后,用户应该能够看到一条确认信息,说明帐户已创建
//display prompt message (confirmation)
//this will show a message to the user showing that the registration has been successful this is not working
func showAlert() {
let alertController = UIAlertController(title: "Thank You", message: "Registration Has Been Completed. Thank You", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true){ () -> Void in
return;
}
}
这也没有出现。我写了关于如何解决这个问题的全部内容。你可以从这里参考它
@IBOutlet var userNameField: UITextField!
@IBOutlet var passwordfield: UITextField!
@IBOutlet var confirmPasswordField: UITextField!
@IBAction func registerButtonTapped() {
if userNameField.text == "" && passwordfield.text == "" && confirmPasswordField.text == "" {
alertController(title: "Error", message: "One or more fields are missing", cancelTitle: "Try again!")
} else {
if passwordfield.text != confirmPasswordField.text {
alertController(title: "Error", message: "Your passwords do not match", cancelTitle: "Try again!")
} else {
//switch views, or do whatever you want when the user correctly enters in their information
}
}
}
func alertController(title: String, message: String, cancelTitle: String) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
let cancelAction = UIAlertAction(title: cancelTitle, style: .cancel)
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)
}
欢迎来到SO!请不要向人们尖叫,并阅读如何提出一个好问题:showAlert在哪里被调用?对于叫喊和乞讨,我投了反对票。我试图使用alert.show(),但收到了大量错误。caps lock纯粹是出于意外,我对此表示歉意,刚刚收到一份作业,我似乎无法克服这个令人沮丧的错误Hi Cyril谢谢你的评论,我已经提出了你所说的所有建议,由于某些原因,当所有字段和密码都正确输入时,它仍然显示错误消息。嗨,我还收到了一个额外的错误,说线程1信号SIGABRITI应该仔细阅读你的问题。我一到电脑就回答。我对我的原始答案做了更改。嗨,西里尔,我尝试过做这些更改,但是这些更改造成了多个错误,无法编译。