gpt4 book ai didi

ios - 警报弹出窗口不显示

转载 作者:行者123 更新时间:2023-11-30 13:11:44 25 4
gpt4 key购买 nike

我的项目中有一些代码:

@IBAction func createAccountAction(sender: AnyObject) {
if self.emailField.text == "" || self.passwordField.text == ""
{
let alertController = UIAlertController(title: "Oops!", message: "Please enter an email and password.", preferredStyle: .Alert)

let defaultAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil)
alertController.addAction(defaultAction)

self.presentViewController(alertController, animated: true, completion: nil)
}
else
{
FIRAuth.auth()?.createUserWithEmail(self.emailField.text!, password: self.passwordField.text!) { (user, error) in

if error == nil
{
let alertController = UIAlertController(title: "Done", message: "Account created!", preferredStyle: .Alert)

let defaultAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil)
alertController.addAction(defaultAction)
self.emailField.text = ""
self.passwordField.text = ""



}
else
{
let alertController = UIAlertController(title: "Oops!", message: error?.localizedDescription, preferredStyle: .Alert)

let defaultAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil)
alertController.addAction(defaultAction)

self.presentViewController(alertController, animated: true, completion: nil)
}

正如您在最后一个 else 语句中看到的,我有一个带有消息 Oops 的 AlertController,当单击 createAccountAction 按钮 时将显示该消息。

但是当用户按下按钮并且没有在文本字段中填写任何内容时,就会发生这种情况。

现在我想要的是,当用户成功填写文本字段时,相同的弹出窗口应该与我指定的其他文本一起出现。

当我运行代码时,它会执行该部分

 self.emailField.text = ""
self.passwordField.text = ""

但不提供 AlertController。

如何才能实现我想要的目标?

最佳答案

你错过了这一行:

 self.presentViewController(alertController, animated: true, completion: nil)

应该在:

之后
self.emailField.text = ""
self.passwordField.text = ""

所以您没有呈现警报。

还有。您可以将整个函数更改为更简单的函数:

@IBAction func createAccountAction(sender: AnyObject) {

if self.emailField.text == "" || self.passwordField.text == "" {

let title = "Oops"
let message = "Please enter an email and password."

let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
let defaultAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil)
alertController.addAction(defaultAction)
self.presentViewController(alertController, animated: true, completion: nil)

} else {
FIRAuth.auth()?.createUserWithEmail(self.emailField.text!, password: self.passwordField.text!) { (user, error) in

var title : String
var message : String

if error == nil {
title = "Done"
message = "Account created!"
self.emailField.text = ""
self.passwordField.text = ""
} else {
title = "Oops!"
message = "error?.localizedDescription"
}

let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
let defaultAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil)
alertController.addAction(defaultAction)
self.presentViewController(alertController, animated: true, completion: nil)

}
}
}

关于ios - 警报弹出窗口不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38645515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com