簡體   English   中英

在解開segue之前顯示UIAlert

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM