Swift:如何从另一个警报调用两次相同的警报?

发布于 2025-01-11 17:31:29 字数 1663 浏览 0 评论 0原文

我已将问题简化为以下代码,其中发生相同的行为。

我想要做的是调用一个包含警报的方法(我的代码中的 messageWindow() )两次,这意味着一个在另一个之后。我想从另一个方法调用该方法,该方法在我的代码中还包含一个警报(userInput())。

import UIKit

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewDidAppear(_ animated: Bool) {
        userInput()
    }
    
    func userInput() {
        let alert = UIAlertController(
            title: "Welcome",
            message: "Do you want say hello?",
            preferredStyle: .alert)
        let actionYes = UIAlertAction(
            title: "Yes",
            style: .default) {_ in
                print("hello")
                self.messageWindow(title: "1st call", message: "Hello!")
                self.messageWindow(title: "2nd call", message: "Hello!!")
            }
        let actionNo = UIAlertAction(
            title: "No",
            style: .default) { (action) in }
        alert.addAction(actionYes)
        alert.addAction(actionNo)
        self.present(alert, animated: true)
    }
    
    func messageWindow (title: String, message: String) {
        let alert = UIAlertController(
            title: title,
            message: message,
            preferredStyle: .alert)
        let actionOk = UIAlertAction(title: "OK", style: .default) { (action) in }
        alert.addAction(actionOk)
        self.present(alert, animated: true)
    }
}

我的问题是第二个调用不会被执行(下面的代码片段)。这意味着我没有看到像第一次调用那样弹出的窗口。

self.messageWindow(title: "2nd call", message: "Hello!!")

我对 Swift 编码还比较陌生。请原谅我的问题,如果这是一个非常简单的问题。我没有找到任何可以帮助我解决这个问题的东西。

我很感激你的帮助。 谢谢。

I have simplified my problem to the following code where the same behavior occurs.

What I want to do is calling a method which includes an alert (messageWindow() in my code) two times which means one behind the other. I want to call that method from another method which also includes an alert (userInput()) in my code.

import UIKit

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewDidAppear(_ animated: Bool) {
        userInput()
    }
    
    func userInput() {
        let alert = UIAlertController(
            title: "Welcome",
            message: "Do you want say hello?",
            preferredStyle: .alert)
        let actionYes = UIAlertAction(
            title: "Yes",
            style: .default) {_ in
                print("hello")
                self.messageWindow(title: "1st call", message: "Hello!")
                self.messageWindow(title: "2nd call", message: "Hello!!")
            }
        let actionNo = UIAlertAction(
            title: "No",
            style: .default) { (action) in }
        alert.addAction(actionYes)
        alert.addAction(actionNo)
        self.present(alert, animated: true)
    }
    
    func messageWindow (title: String, message: String) {
        let alert = UIAlertController(
            title: title,
            message: message,
            preferredStyle: .alert)
        let actionOk = UIAlertAction(title: "OK", style: .default) { (action) in }
        alert.addAction(actionOk)
        self.present(alert, animated: true)
    }
}

My problem is that the second call won't be executed (code snippet below). That means I don't see a window popping up like in the first call.

self.messageWindow(title: "2nd call", message: "Hello!!")

I'm relative new in coding with Swift. Please excuse my question in case it is a really simple one. I didn't found anything which helped me solving this problem.

I appreciate your help.
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文