【发布时间】:2019-10-07 15:49:32
【问题描述】:
我正在尝试链接一个特定的视图控制器,该视图控制器将在用户从选择随机数后自动生成的不同弹出窗口中选择“是”时运行。
如何仅在用户选择“是”时添加第二个视图控制器(并在用户选择“否”时丢弃弹出窗口)?
我已经在 Stack 上进行了研究,有人建议使用 Storyboard 和 Segues,所以我使用“Show Segue to Weapon Pop Up View Controller”连接了第一个和第二个视图控制器,第二个 VC 的故事板 ID 为“PopUp ”。
感谢您的帮助!
这是我的代码:
@IBAction func rolld20(_ sender: Any) {
let randomnumber20 = Int.random(in: 1...20)
d20label.text = String(randomnumber20)
//create the alert
let d20alert = UIAlertController(title: "You rolled a D20!",
message: "You rolled a \(String(randomnumber20))! Is this enough?", preferredStyle: UIAlertController.Style.alert)
// add the actions (yes/no buttons)
d20alert.addAction(UIAlertAction(title: "Yes", style: UIAlertAction.Style.default, handler: nil)) {
//code to open up second view controller with the storyboard ID "PopUp" goes here I believe?
}
d20alert.addAction(UIAlertAction(title: "No", style: UIAlertAction.Style.cancel, handler: nil))
// show the alert when the user clicks to roll d20
self.present(d20alert, animated: true, completion: nil)
}
【问题讨论】: