uinavigationcontrollerunwind-seguexcode10beta6

unwind segue Xcode 10 swift with navigation controller not working


I have pursued the internet for an answer and following steps in apple documentation but to no avail. I have two view controllers. One which contains a list of favourite items (FavouriteTableViewController), another which contains the details of the item being added to the list of favourites (ExplorationDetailViewController).

I have a save button on the navigation bar (from a navigation controller scene) in the ExplorationDetailViewController that I want to click to kick off the unwind segue. I have carried out the following steps, but when I click on the save button, it is completely unresponsive. I am including the most relevant code for the unwind.

I have created prepareforsegue function in ExplorationResultViewController from where I will be passing data for an image and a label to the other ViewController as part of the unwind segue

class ExplorationResultViewController: UIViewController {
    var exploration_result: Favourites?
    @IBOutlet weak var ExplorationLabel: UILabel!
    @IBOutlet weak var ExplorationPhoto: UIImageView!

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        // Pass the selected object to the new view controller.
        super.prepare(for: segue, sender: sender)


        let photo = ExplorationPhoto.image
        let name = ExplorationLabel.text ?? ""

        exploration_result = Favourites(name: name, photo: photo)
    }

Then in the FavouriteTableViewController, I have added an unwindtoHere method which adds image and label to the list.

class FavouritesTableViewController: UITableViewController {

    // MARK: Properties
    var favourites = [Favourites]()

    // MARK: Actions
    @IBAction func unwindToFavouriteList(sender: UIStoryboardSegue) {
        if let sourceViewController = sender.source as? ExplorationResultViewController, let favourite = sourceViewController.exploration_result{
            // Add a new meal.
            let newIndexPath = IndexPath(row: favourites.count, section: 0)
            favourites.append(favourite)
            tableView.insertRows(at: [newIndexPath], with: .automatic)

        }

Then going into my storyboard, I control+drag the Save button in the ExplorationDetailViewController and connect it to the exit item at the top of the scene. My unwind method appears in the pop up as: 'unwindToFavouriteListWithSender'. I select this.

I should be good to go now, but when I run the simulator and click the Save button, it is absolutely unresponsive. See StoryBoard Design Please help


Solution

  • From your comments, I'm guessing that the problem is that there is no FavouritesTableViewController in the view controller hierarchy at the time that the Save button is pressed. You cannot unwind to something that isn't already present: "unwind" means go back to something that was instantiated earlier and remains present, above the current view controller in the view controller hierarchy.

    For example, let's say we have

    UINavigationController -> (root view controller) -> FavouritesTableViewController
        -> (push) ExplorationResultViewController
    

    Now we can unwind from the Exploration controller to the Favourites controller, because the Favorites controller is already in the hierarchy before the Exploration controller. It sounds like maybe you are not appreciating that fact.