在解开segue之前显示UIAlert

时间:2018-01-02 16:41:03

标签: ios swift segue unwind-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) {

}

2 个答案:

答案 0 :(得分:2)

不是直接执行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操作即可执行此方法,如果您在UIAlertViewController通过self.performSegue(withIdentifier: #<SegueIdentifier>, sender: #<sender>)

按取消,则会执行您的segue

答案 1 :(得分:1)

首先,使用两个选项发出警告:

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然后命名它(也可以通过控制从视图控制器黄色图标拖动到另一个视图控制器来连接它):

enter image description here

之后把这个代码放到执行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)
        }

    }
}