I hava a variable named index that decorated @State, and I want update value when click setting toolbar button.
The toolbar button’s block is executed, but the value of index still is -1.
I debug and check out the value of currentIndex, it still is -1.
import SwiftUI
import SwiftData
struct HomeView: View {
@State var isPresentingAddView = false
@State var index = -1
var body: some View {
NavigationStack() {
VStack(alignment: .leading) {
Spacer()
}
.navigationTitle(Text("title"))
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
Button {
index = 1
isPresentingAddView = true
} label: {
Label("setting", systemImage: "plus")
}
}
}
}
.sheet(isPresented: $isPresentingAddView) {
let currentIndex = index
}
}
}
The toolbar button block code is execute before the sheet block code




