Swift:如何从另一个警报调用两次相同的警报?
我已将问题简化为以下代码,其中发生相同的行为。
我想要做的是调用一个包含警报的方法(我的代码中的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论