如何使用 AlertView 进行 segue



这是警报的代码。问题是当用户按下"Ja"按钮时,我想转到另一个 VC,该按钮在英语中表示"是"。

@IBAction func TillbakaAction(_ sender: UIButton)
{
     createAlert(title: "Är du säker på att du vill börja om?", message: "Ifyllda betyg nollställs")

}
func createAlert (title:String, message:String)
{
    let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
    //CREATING ON BUTTON
    alert.addAction(UIAlertAction(title: "Ja", style: UIAlertActionStyle.default, handler: { (action) in
        alert.dismiss(animated: true, completion: nil)
        print ("Jag vill gå tillbaka")

                }))
    alert.addAction(UIAlertAction(title: "Nej", style: UIAlertActionStyle.default, handler: { (action) in
        alert.dismiss(animated: true, completion: nil)
        print("Nej, jag vill inte gå tillbaka")
    }))
    self.present(alert, animated: true, completion: nil)

无需调用带有警报的dismiss,当您按下 AlertController 的任何操作时,它将自动关闭警报。

因此,只需在您的操作中添加performSegue(withIdentifier:sender:)即可。

let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ja", style: .default, handler: { (action) in
    print ("Jag vill gå tillbaka")
    // call the segue at hare
    self.performSegue(withIdentifier:"SegueIdentifer", sender: nil)
}))
alert.addAction(UIAlertAction(title: "Nej", style: .default, handler: { (action) in
    print("Nej, jag vill inte gå tillbaka")
}))
self.present(alert, animated: true)
@IBAction func TillbakaAction(_ sender: UIButton)
{
     createAlert(title: "Är du säker på att du vill börja om?", message: "Ifyllda betyg nollställs")

}
func createAlert (title:String, message:String)
{
    let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
    //CREATING ON BUTTON
    alert.addAction(UIAlertAction(title: "Ja", style: UIAlertActionStyle.default, handler: { (action) in
        alert.dismiss(animated: true, completion: nil)
        print ("Jag vill gå tillbaka")
// call the segue at hare

                }))
    alert.addAction(UIAlertAction(title: "Nej", style: UIAlertActionStyle.default, handler: { (action) in
        alert.dismiss(animated: true, completion: nil)
        print("Nej, jag vill inte gå tillbaka")
    }))
    self.present(alert, animated: true, completion: nil)

最新更新