type the following into the textfield “Add nintendo switch”.
“nintendo” should be autocorrected to “Nintendo” and press the “send” button.
Textfield binded state var will clear but not render as cleared in the UI when done so via the button action closure
However, the textfield will clear if done so via the onSubmit view modifier closure
Is there something I am missing to get this to work correctly?
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()
}
}
wrapping in a dispatchQueue.main.async call
using onSubmit view modifier
the UI should update and render the textfield as empty
user25107564 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.