[英]Show UIAlert before unwinding segue
我正在尝试触发警告,询问您是否要在取消后保存或删除草稿。 我很亲密,但我似乎无法做对。
我正从'ReplyMailViewController'(ViewController A)展开到'MailContentViewController'(ViewController B)。
我在ViewController A中添加了以下代码来显示警报并“保持”segue执行:
override func shouldPerformSegue(withIdentifier identifier: String?, sender: Any?) -> Bool {
if let ident = identifier {
if ident == "cancelDraft" {
let saveDraftActionHandler = { (action:UIAlertAction!) -> Void in
NSLog("EXIT")
}
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let deleteDraftAction = UIAlertAction(title: "Delete Draft", style: .destructive, handler: nil)
alertController.addAction(deleteDraftAction)
let saveDraftAction = UIAlertAction(title: "Save Draft", style: .default, handler: saveDraftActionHandler)
alertController.addAction(saveDraftAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)
return false
}
}
return true
}
segue保留了这段代码,但问题是我在按下“保存草稿”之后无法弄清楚如何继续展开segue。
我在View Controller B中也有一个展开功能,但我似乎无法弄清楚如何将这个用于此任务:
@IBAction func cancelToMailContentViewController(_ segue: UIStoryboardSegue) {
}
而不是直接执行segue,您需要首先显示您的UIAlertViewController
并根据用户响应执行您的segue
与否
@IBAction func showAlertViewController(){
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let replyAction = UIAlertAction(title: "Delete Draft", style: .destructive, handler: nil)
let replyAllAction = UIAlertAction(title: "Save Draft", style: .default) { (action) in
//Do whatever you need here
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
self.performSegue(withIdentifier: "cancelDraft", sender: action) //executing the segue on cancel
}
alertController.addAction(replyAllAction)
alertController.addAction(replyAction)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)
}
在此之后,您只需要更改展开segue操作以执行此方法,如果您通过self.performSegue(withIdentifier: #<SegueIdentifier>, sender: #<sender>)
在UIAlertViewController
按下取消,则会执行您的segue self.performSegue(withIdentifier: #<SegueIdentifier>, sender: #<sender>)
首先,使用两个选项发出警报:
class ViewController: UIViewController {
@IBAction func showAlertButtonTapped(_ sender: UIButton) {
// create the alert
let alert = UIAlertController(title: "UIAlertController", message: "Save this work?", preferredStyle: UIAlertControllerStyle.alert)
// add the actions (buttons)
alert.addAction(UIAlertAction(title: "Hell Yeah", style: UIAlertActionStyle.default, handler: nil))
alert.addAction(UIAlertAction(title: "Hell No", style: UIAlertActionStyle.cancel, handler: nil))
// show the alert
self.present(alert, animated: true, completion: nil)
}
在此之后,你必须制作segue然后命名它(也可以通过控制从视图控制器黄色图标拖动到另一个视图控制器来连接它):
之后把这个代码用来执行segue:
self.performSegue(withIdentifier: ":)", sender: self)
之后,当用户响应警报时,您将执行segue:
if buttonTitle == "Hell Yeah" {
elf.performSegue(withIdentifier: ":)", sender: self)
}
所以,最后,您的代码应如下所示:
class ViewController: UIViewController {
@IBAction func showAlertButtonTapped(_ sender: UIButton) {
// create the alert
let alert = UIAlertController(title: "UIAlertController", message: "Save this work?", preferredStyle: UIAlertControllerStyle.alert)
// add the actions (buttons)
alert.addAction(UIAlertAction(title: "Hell Yeah", style: UIAlertActionStyle.default, handler: nil))
alert.addAction(UIAlertAction(title: "Hell No", style: UIAlertActionStyle.cancel, handler: nil))
// show the alert
self.present(alert, animated: true, completion: nil)
if buttonTitle == "Hell Yeah" {
self.performSegue(withIdentifier: ":)", sender: self)
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.