Post

Replies

Boosts

Views

Activity

how to assign textField's default value in list (swiftUI)
I have a list with textField and a button. my goal is that I want to provide default value to textField when I toggle the button. I can do it with text, but I don't know how to do with textField struct ContentView: View { @State var isEnableBatch: Bool = false var body: some View { VStack { Button { isEnableBatch.toggle() } label: { Image(isEnableBatch == true ? "EAIcon-Check_rectangle" : "EAIcon-Check_Box") } List { ForEach(0 ..< 10, id: \.self) { index in let value = isEnableBatch ? "batch Enable" : "\(index)" Text(value) // <-- I don't know how to do here if i change to textField } } .listStyle(.plain) } .padding() } } If I change to use textField as below it will show compile error No exact matches in reference to static method 'buildExpression' struct ContentView: View { @State var isEnableBatch: Bool = false @State var attValue: String = "" var body: some View { VStack { Button { isEnableBatch.toggle() } label: { Image(isEnableBatch == true ? "EAIcon-Check_rectangle" : "EAIcon-Check_Box") } List { ForEach(0 ..< 10, id: \.self) { index in attValue = isEnableBatch ? "batch Enable" : "\(index)" // <-- how to update TextField's default value ?? TextField("", text: $attValue) } } .listStyle(.plain) } .padding() } }
2
0
569
Aug ’23