My problem is as follows:
My view has a textfield and a disabled button along with two states.
One state for the value in the textfield and one state for the disabled button.
I want to enable the button when something (at least 1 caracter) is entered in the textfield, and redisables it if the user deletes the content.
I have the idea working,sorts of. I capture the initial hit on the field, and the result on the commit(return) on the field.
But I cannot find how to know if the field is empty or not.
Any help will be most welcome
PS I know that there is a Combine way as presented in one of the WWDC 19 video, but I would like to have it work differently.
Best regards Lars
import SwiftUI
struct Avirer: View {
@State var isDisabled: Bool = true
@State var lab: String = ""
func editChange() {
//this function is activated when the field is touched self.isDisabled.toggle()
}
var body: some View {
VStack {
HStack{
Text("Enter Key :")
TextField("Placeholder", text: $lab, onEditingChanged:{ _ in self.editChange()},
onCommit: {EmptyView()})
}
Button(action: {self.isDisabled.toggle() }) {
Text("Activate the submit button")
}
// Just an indicator that the state variable works correctly
Text(lab)
Button(action: /*@START_MENU_TOKEN@*/{}/*@END_MENU_TOKEN@*/) {
Text("Submit")
}.disabled(isDisabled)
}.padding(20)
}
}
struct Avirer_Previews: PreviewProvider {
static var previews: some View {
Avirer()
}
}