简体   繁体   English

在解开segue之前显示UIAlert

[英]Show UIAlert before unwinding segue

I'm trying to fire an alert that asks if you want to save or delete a draft after pressing cancel. 我正在尝试触发警告,询问您是否要在取消后保存或删除草稿。 I'm quite close, but I can't seem to get it right. 我很亲密,但我似乎无法做对。

I'm unwinding from 'ReplyMailViewController'(ViewController A) to 'MailContentViewController'(ViewController B). 我正从'ReplyMailViewController'(ViewController A)展开到'MailContentViewController'(ViewController B)。

I added the following code in ViewController A to show the alert and 'hold' the segue perform: 我在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
}

The segue holds with this code, but the issue is that I can't figure out how to continue the unwind segue after pressing 'Save Draft' for example. segue保留了这段代码,但问题是我在按下“保存草稿”之后无法弄清楚如何继续展开segue。

I also have an unwind function in View Controller B, but I can't seem to figure out how I can use this one for this task: 我在View Controller B中也有一个展开功能,但我似乎无法弄清楚如何将这个用于此任务:

@IBAction func cancelToMailContentViewController(_ segue: UIStoryboardSegue) {

}

Instead of perform the segue directly you need to show your UIAlertViewController first and according to the user response execute your segue or not 而不是直接执行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)

}

After this you only need to change the unwind segue action to execute this method, and your segue will be executed if you press cancel in the UIAlertViewController via self.performSegue(withIdentifier: #<SegueIdentifier>, sender: #<sender>) 在此之后,您只需要更改展开segue操作以执行此方法,如果您通过self.performSegue(withIdentifier: #<SegueIdentifier>, sender: #<sender>)UIAlertViewController按下取消,则会执行您的segue self.performSegue(withIdentifier: #<SegueIdentifier>, sender: #<sender>)

First, make the alert with two options: 首先,使用两个选项发出警报:

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

After this, you have to make the segue and then name it (also connect it by control dragging from the view controller yellow icon to the other view controller): 在此之后,你必须制作segue然后命名它(也可以通过控制从视图控制器黄色图标拖动到另一个视图控制器来连接它):

在此输入图像描述

After that put this your code to execute the segue: 之后把这个代码用来执行segue:

self.performSegue(withIdentifier: ":)", sender: self)

After that you are going to execute the segue when the user responds to the alert: 之后,当用户响应警报时,您将执行segue:

if buttonTitle == "Hell Yeah" {
    elf.performSegue(withIdentifier: ":)", sender: self)
}

so, in the end, your code should look like this: 所以,最后,您的代码应如下所示:

 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM