Finally, I found how to solve my problems
struct ContentView: View {
@State var isEnableBatch: Bool = false
@State var attValues: [String] = ["", "" , ""] // <- use an array to store every state value
var body: some View {
VStack {
Button {
isEnableBatch.toggle()
// update state value while toggling the button
if isEnableBatch {
for index in 0 ..< 3 {
attValues[index] = "batch Enable"
}
} else {
for index in 0 ..< 3 {
attValues[index] = ""
}
}
} label: {
Image(isEnableBatch == true ? "EAIcon-Check_rectangle" : "EAIcon-Check_Box")
}
List {
ForEach(0 ..< 3, id: \.self) { index in
TextField("", text: $attValues[index]) // <-- find the corresponding state value to binding
}
}
.listStyle(.plain)
}
.padding()
}
}
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: