I am attempting to make a recipe app similar to the one used in the iOS Foundations Course, but instead of the List View presenting like a Navigation Link with a Back Button, I want it to display a sheet like the Featured Recipe View does in the course’s app for each recipe I have.
I’ve tried pulling the relevant (in my opinion) code from each piece to make this work, but my current problem is that when I press the button created for each different recipe, the sheet that comes up only ever displays the first recipe in the index (position 0).
I have the @State var for this:
@State var isDetailViewShowing = false
I’m looping through each recipe and my Button’s action is set to toggle my @State var, and I have the .sheet modifier selected with the same isPresented parameters as the course’s recipe app:
ForEach(0..<model.recipes.count) { index in
Button {
self.isDetailViewShowing.toggle()
} label: {
// Recipe card
ZStack (alignment: .leading) {
Rectangle()
.foregroundStyle(.ultraThinMaterial)
HStack(spacing: 10) {
Image(model.recipes[index].image)
.resizable()
.scaledToFill()
.frame(width: 90, height: 90, alignment: .center)
.clipped()
.cornerRadius(10)
VStack (alignment: .leading) {
Text(model.recipes[index].name)
.font(.title3)
.multilineTextAlignment(.leading)
HStack {
Image(systemName: "timer")
.resizable()
.frame(width: 15, height: 15)
Text(model.recipes[index].totalTime)
.font(.caption)
.fontWeight(.light)
.padding(.trailing, 3)
Image(systemName: "heart.text.square")
.resizable()
.frame(width: 15, height: 15)
Text("\(model.recipes[index].calories) calories")
.font(.caption)
.fontWeight(.light)
}
}
}
}
}
.sheet(isPresented: $isDetailViewShowing) {
// Show the Recipe Detail View
RecipeDetailView(recipe: model.recipes[index])
I can’t figure this out and would love help in this area! Thanks so much in advance!