在展开 segue 之前显示 UIAlert

Show UIAlert before unwinding segue

我正在尝试发出警报,询问您是否要在按取消后保存或删除草稿。我已经很接近了,但我似乎做对了。

我正在从 'ReplyMailViewController'(ViewController A) 放松到 'MailContentViewController'(ViewController B)。

我在 ViewController A 中添加了以下代码以显示警报和 'hold' 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 与此代码保持一致,但问题是我不知道如何在按下 'Save Draft' 后继续展开 segue。

我在 View Controller B 中也有一个展开功能,但我似乎无法弄清楚如何使用它来完成此任务:

@IBAction func cancelToMailContentViewController(_ segue: UIStoryboardSegue) {

}

您需要先显示您的 UIAlertViewController,然后根据用户响应执行您的 segue 或不执行

,而不是直接执行 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)

}

在此之后你只需要改变unwind segue动作来执行这个方法,如果你在UIAlertViewController中通过self.performSegue(withIdentifier: #<SegueIdentifier>, sender: #<sender>)

按取消,你的segue将被执行

首先,使用两个选项进行提醒:

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)
        }

    }
}