I know that you can't use a sheet inside of a ForEach view but my workaround outputs me most of the time the right index but sometimes just the first.
@State private var isEditPresented: Bool = false
@State private var cardIndex: Int = 0
var body: some View {
ForEach(lesson.cards.indices, id: \.self) { index in
Button {
cardIndex = index
print("edit: \(cardIndex)")
isEditPresented = true
} //:BUTTON
}.sheet(isPresented: $isEditPresented) {
EditCardView(cardIndex: cardIndex)
} //:FOREACH
} //:BODY
The print statement outputs:
edit: 1
My EditCardView:
@State var cardIndex: Int
var body: some View {
Form {
SomeViews()
}.onAppear() {
print("editView: \(cardIndex)")
} //:FORM
} //:BODY
The interesting thing is that the first print statement is edit: 1
but the print statement from my EditCardView returns editView: 0
Sometimes it works fine and prints:
edit: 1
editView: 1
How do I fix that?