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!