Hello
I make recipe apps. I configured my pickerView and gave it Recipe name. What I want, is by clicking on my "Button", it's having the Recipe written on my "TextView" on another ViewController.
A little help will be cool
thank you
google traduction
You said: this line destVC.Lavue = choix[name] ?? "" doesn't wor, an error message is displayed
Which error ?
Take care: selectedRow is in Int, so you need to find the name associated in choix[selectedRow]
How have you defined the class View2G (what I called RecipeViewController)?
Should have a property
var recipeText : String? // LaVue is not a meaningful name, I propose you recipeText insteadThe 2 var LeChoix and choix are different: one holds the list of recipes (array), the other the texts for recipes (dictionary).
You should also give meaningful names (as recipesDict), which start with lowerCase (Swift convention) (leChoix)
And you can initialize the dictionary at declaration or in viewDidload, that's the same.
You should have something like this:
in first ViewController,
var leChoix = ["Cake","Muffins"] // it's the name of the product for my pickerView
var recipesDict : [String:String] = [:] // That will hold the texts for recipês themselvesin viewDidload
recipesDict["Cake"] = "Take 200g of flour, 4 eggs…" // Take care of uppercase in keys
recipesDict["Muffins"] = "150g Butter…"Note: this could also be done instead at declaration:
var recipesDict : [String:String] = ["cake":"Take 200g of flour, 4 eggs…", "Muffins": "150g Butter…"]Now in prepare
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "GoToRecipe" {
if let destVC = segue.destination as? RecipeViewController { // Maybe you named it View2G ; if so as? View2G
let selected = choixGouter.selectedRow(inComponent: 0
if selected >= 0 && selected < leChoix.count { // Should always be true except if click out of bounds of picker
let recipeName = leChoix[selected] // theTextYouSelectedInPicker, for instance "Cake"
destVC.recipeText = recipesDict[recipeName] ?? ""
}
}
}
}Some picker functions are wrong.
Should be
func numberOfComponents(in pickerView: UIPickerView) -> Int{
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
return leChoix.count // NOT choix.count, normally the same, but you never know
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?{
return leChoix[row] // OK, in lowercase
}