import SwiftUI
struct ContentView: View {
let disciplines = ["Potato", "Tomato", "Onion"]
var body: some View {
NavigationView{
List(disciplines, id: \.self) { discipline in
NavigationLink(
destination: DetailView(discipline: discipline)) {
Text(discipline)
}
}
.navigationBarTitle (Text("The App Title"), displayMode: .inline)
}
}
}
struct DetailView: View {
let discipline: String
var body: some View {
Text(discipline)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Right now for example, when I tap "Potato" in the List, the Text in the View will show "Potato". But I want to have a different Text for each one of the Listed, not the same word.
How can I do that with SwiftUI?
Thank you.
You can create an ingredient
object and have two fields. One for the name
and one for description
struct Ingredient: Identifiable {
var id = UUID()
var name: String
var description: String
}
with this you would have to modify your disciplines
to the following to take ingredient
objects rather than strings
let disciplines = [Ingredient(name: "Potato", description: "Yummy potato"),
Ingredient(name: "Tomato", description: "Yummy Tomato"),
Ingredient(name: "Onion", description: "Yummy Onion")]
and your view would change slightly to use the name
and description
fields from the ingredient
object
var body: some View {
NavigationView{
List(disciplines) { discipline in
NavigationLink(
destination: DetailView(discipline: discipline.description)) {
Text(discipline.name)
}
}
.navigationBarTitle (Text("The App Title"), displayMode: .inline)
}
}