Can’t pass data correctly to modal presentation using ForEach and CoreData in SwiftUI
There should be only one sheet in view stack, so just move it out of ForEach
, like below
struct CarScrollView: View {
@Environment(\.managedObjectContext) var moc
@FetchRequest(entity: Cars.entity(), sortDescriptors: []) var cars: FetchedResults<Cars>
@State private var selectedCar: Car? = nil
var body: some View {
VStack {
ScrollView (.vertical, showsIndicators: false) {
ForEach (cars, id: \.self) { car in
Text("\(car.text!)")
.onTapGesture {
self.selectedCar = car
}
}
}
}
.sheet(item: self.$selectedCar) { car in
CarDetail(id: car.id, text: car.text)
}
}
}