这是警报的代码。问题是,当用户按下“Ja”按钮时,我想转到另一个VC,这在英语中是“Yes”的意思。
@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)
关于swift - 如何使用AlertView进行搜索?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42041700/