I am trying to create a view in SwiftUI where by clicking on a button, a sheet is presented. A simplified version of the code looks like this:
struct ExampleView: View {
@State private var selectedIndex: Int = -1
@State private var showSheet: Bool = false
var body: some View {
Text("Tap me!")
.onTapGesture {
selectedIndex = 2
showSheet = true
}.sheet(isPresented:$showSheet){
VStack {
Text("Selected Index: (selectedIndex)")
}
.onAppear {
print("Sheet appeared: selectedIndex = (selectedIndex)")
}
}
}
}
When I tap on the button, a view is presented with SelectedIndex: -1
; however, in the print statement, the correct value appears(Sheet appeared: selectedIndex = 2
).
What I would like is for the correct value (2
) to be passed when creating the view being presented — how can I achieve that?
7