简体   繁体   中英

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

I added the following code in ViewController A to show the alert and 'hold' the segue perform:

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.

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:

@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

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

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

在此输入图像描述

After that put this your code to execute the segue:

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

After that you are going to execute the segue when the user responds to the alert:

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

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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