Showing alert after unwind segue stops the segue. How do I make sure alert is shown after unwind segue is completed?

偶尔善良 提交于 2019-12-10 15:43:57

问题


I have an unwind segue from A view controller to B view controller.

A network operation is done in B. After the operation is completed, the response will be shown in A view controller.

I successfully made this structure. However there is an issue:

When I try to show the alert, it shows but stops the segue. How do i make sure alert shows after segue is completed.

The error is here:

2016-04-27 14:39:28.350 PROJECT[9100:128844] Presenting view controllers on detached view controllers is discouraged <PROJECT.FeedTableViewController: 0x7a928c00>.
2016-04-27 14:39:28.359 PROJECT[9100:128844] popToViewController:transition: called on <UINavigationController 0x7c12a800> while an existing transition or presentation is occurring; the navigation stack will not be updated.

Unwind handler in A:

@IBAction func unwindToFeed(segue: UIStoryboardSegue) {
        jsonArray[rowFromShare!]["ApplicationDataUsers"] = jsonFromShare!
        tableView.reloadData()
        ShowErrorDialog("Success", message: successMessageFromShare!, buttonTitle: "OK")
    }

//Error Dialog
func ShowErrorDialog(title:String, message:String, buttonTitle:String){
    let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)
    alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
    self.presentViewController(alert, animated: true){}
}

Unwind trigger in B:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "unwindToFeed"{
        let feedTable = segue.destinationViewController as! FeedTableViewController
        feedTable.rowFromShare = row
        feedTable.jsonFromShare = jsonToShare
        feedTable.successMessageFromShare = successMessageToShare
    }

    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}

A = FeedTableViewController B = ShareTableViewController

How do I make sure alert is shown after segue is done?


回答1:


The unwindToFeed method is called before the unwind segue is complete, as you have found.

One approach would be to set a boolean in the unwindToFeed method and then check this boolean in viewDidAppear, when you know the segue is complete. If the boolean is set then you can display the alert:

@IBAction func unwindToFeed(segue: UIStoryboardSegue) {
    jsonArray[rowFromShare!]["ApplicationDataUsers"] = jsonFromShare!
    tableView.reloadData()
    self.unwinding = true
}

override func viewDidAppear(animated: Bool) {
   super.viewDidAppear(animated)
   if (self.unwinding) {
       self.ShowErrorDialog("Success", message: successMessageFromShare!, buttonTitle: "OK")
   self.unwinding=false
   }


来源:https://stackoverflow.com/questions/36888812/showing-alert-after-unwind-segue-stops-the-segue-how-do-i-make-sure-alert-is-sh

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!