swift - 在警报中显示警报

标签 swift alert uialertview

我正在尝试显示接受用户输入然后弹出给出的文本的警报

@IBAction func forgotPassword(_ sender: Any) {
    //1. Create the alert controller.
    let alert = UIAlertController(title: "Email Recovery", message: "Enter your email to recover your account", preferredStyle: .alert)

    //2. Add the text field. You can configure it however you need.
    alert.addTextField { (textField) in
        textField.text = ""
    }

    // 3. Grab the value from the text field, and print it when the user clicks OK.
    alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { [weak alert] (_) in
        let textField = alert?.textFields![0] // Force unwrapping because we know it exists.
        print("An email has been sent to \(String(describing: textField?.text)) for account recovery")
    }))

    // 4. Present the alert.
    self.present(alert, animated: true, completion: nil)
}

输入和输出工作正常,但它没有给出另一个警报,只是在控制台中打印我需要的内容。

我错过了什么吗?

最佳答案

我猜你误解了一点:“打印”只是在你的控制台上打印,如果你想在点击“确定”按钮后打开一个新的警报,你可能需要用以下代码来完成你的代码:

alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { [weak alert, weak self] (_) in
  let message = "An email has been sent to \(alert?.textFields?.first?.text ?? "") for account recovery"
  let innerAlert = UIAlertController(title: nil, message: message, preferredStyle: .alert)
  innerAlert.addAction(UIAlertAction(title: "OK", style: .default, handler:nil))
  self?.present(innerAlert, animated: true, completion: nil)
}))

关于swift - 在警报中显示警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47734741/

相关文章:

swift - 我在滚动时更改了 UITableViewCell 不透明度,但用户交互有一两秒的延迟。我怎样才能解决这个问题?

ios - ios模拟器上的SIGABRT错误

swift - “ fatal error :在展开可选值时意外发现nil”是什么意思?

javascript - 重写全局alert()函数在Tampermonkey中不起作用

java - driver.switchTo().alert().dismiss();无法在 MS Edge 上运行

ios - 我应该放入哪个 AlertView 按钮 "cancel"

ios - 热衷于使用 UITableViewCell 中的数据制作 UIAlert?

ios - 当用户点击 UIAlertView 的 OK 按钮时移动到另一个 View

JSON SWIFT OpenWeatherMapAPI

javascript - Twitter Bootstrap 警报消息关闭并再次打开