Is there a SwiftUI equivalent for textFieldShouldReturn? I would like my app to run a command after I press the return/enter key in a TextField. macOS App
For Big Sur, just use TextField's onCommit closure. For later versions, check out onSubmit(of:_:).
struct ContentView: View {
@State var text = ""
var body: some View {
TextField("Text", text: $text) { isEditing in
print("Textfield is editing: \(isEditing)") /// in case you want to update your UI
} onCommit: {
print("Return pressed") /// for your logic
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With