Jacob Smith
Jacob Smith

Reputation: 813

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

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?

Upvotes: 4

Views: 1223

Answers (1)

Paulw11
Paulw11

Reputation: 114865

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
   }

Upvotes: 1

Related Questions