Problem with connections in Swift

0

I have a problem with a project that I am developing in Swift, it is for class and I am faced with a problem that I can solve with more than I try. I explain:

From the FoodTracker project developed in the Apple Developer Guide I am trying to create a Tab Bar Controller that in one view shows the name of the food, in another the photo and in another the score. The problem is that for me to show this data, in the class MealTableViewController.swift there is a method called prepare which is what makes me show the data in the new view, but just as they do in the guide that I said before, I can not get that to work. I have left the code as I have it in a github repository .

The method that I mention is now like this:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
    super.prepare(for: segue, sender: sender)

    switch(segue.identifier ?? "") {

    case "AddItem":
        os_log("Adding a new meal.", log: OSLog.default, type: .debug)

    case "ShowDetail":
        guard let mealDetailViewController = segue.destination as? NameMealViewController else {
            fatalError("Unexpected destination: \(segue.destination)")
        }

        guard let selectedMealCell = sender as? MealTableViewCell else {
            fatalError("Unexpected sender: \(String(describing: sender))")
        }

        guard let indexPath = tableView.indexPath(for: selectedMealCell) else {
            fatalError("The selected cell is not being displayed by the table")
        }

        let selectedMeal = meals[indexPath.row]
        mealDetailViewController.meal = selectedMeal

    default:
        fatalError("Unexpected Segue Identifier; \(String(describing: segue.identifier))")
    }
}

The fact is that I know it is wrong because the segue.destination is not NameMealViewController since the segue goes to a UITabBarController but this way I can not pass the food that has been selected.

I need help to solve this since I've been thinking about how to do it for a couple of weeks now and I do not get it.

Thank you very much for everything. I recommend downloading the repository to see better what is the problem that I comment.

Greetings!

    
asked by Alfred 21.04.2018 в 12:38
source

1 answer

2

Hello the solution to your problem is in fact very simple. All you have to do is modify the prepare(for segue: UIStoryboardSegue, sender: Any?) method in the following way.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
    super.prepare(for: segue, sender: sender)

    switch(segue.identifier ?? "") {

    case "AddItem":
        os_log("Adding a new meal.", log: OSLog.default, type: .debug)

    case "ShowDetail":
        guard let tabBarController = segue.destination as? UITabBarController else {
            fatalError("Unexpected destination: \(segue.destination)")
        }
        guard let mealDetailViewController = tabBarController.viewControllers?.first as? NameMealViewController else {
            fatalError("Couldn't instantiate NameMealViewController from tabbar")
        }

        guard let selectedMealCell = sender as? MealTableViewCell else {
            fatalError("Unexpected sender: \(String(describing: sender))")
        }

        guard let indexPath = tableView.indexPath(for: selectedMealCell) else {
            fatalError("The selected cell is not being displayed by the table")
        }

        let selectedMeal = meals[indexPath.row]
        mealDetailViewController.meal = selectedMeal

    default:
        fatalError("Unexpected Segue Identifier; \(String(describing: segue.identifier))")
    }
}

The modification made is to cast the segue.destination to a ViewController of class UITabBarController and to set the your ViewController NameMealViewController from the first ViewController of UITabBarController .

I knew it was the first ViewController of TabBar, because I opened your storyboard and it was the first from left to right.

Greetings

    
answered by 22.04.2018 / 01:50
source