4

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?

Moin Shirazi
  • 4,372
  • 2
  • 26
  • 38
Jacob Smith
  • 803
  • 5
  • 13
  • 23
  • Set a boolean property, say, `unwinding` in `unwindToFeed`, then in `viewDidAppear` in view controller A, check if `unwinding` is true, and display the alert if it is. – Paulw11 Apr 27 '16 at 11:48
  • This is late but you can try doing this http://stackoverflow.com/a/37602422/1552116 if you don't want to use state flags – wyu Jun 02 '16 at 21:30

1 Answers1

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
   }
Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • 1
    if you want to avoid using state flags you can consider this approach http://stackoverflow.com/a/37602422/1552116 – wyu Jun 02 '16 at 21:31
  • Unfortunately this does not work for modally presented VCs that don't cover the whole screen. When they are dismissed, the presenting VC does not fire the `viewDidAppear` method. – Phontaine Judd Jan 21 '21 at 02:44