I have a simple text field and a button
the textfield text
var is binded to my @state string var $strText
.
the button has an action that sets strText = ""
when trying to input "nintendo switch" without a capital N, the textField will auto correct to "Nintendo switch". When pressing the button, the textfield will not clear.
I have tried wrapping the button action in a DispatchQueue.main.async but there is no change in behavior.
However, adding an .onSubmit{ }
view modifier to the text field and calling strText = ""
in the onSubmit closure will clear the textField.
Is there anyway to have the button action call the textfield's submit trigger?
import SwiftUI
struct ContentView: View {
@State var text: String = ""
private func clearText() {
print("YAHOOOO 2clear \(self.text)")
self.text = ""
print("YAHOOOO 2\(self.text)")
}
var body: some View {
VStack {
TextField("Message", text: $text, axis: .vertical)
.autocorrectionDisabled(false)
.onSubmit {
clearText()
}
Button(action: {
DispatchQueue.main.async {
clearText()
}
}, label: {
Text("Send")
})
}
.padding()
}
}