Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI equivalent to textFieldShouldReturn

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

like image 979
WilliamD47 Avatar asked Dec 05 '25 14:12

WilliamD47


1 Answers

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
        }
    }
}
like image 114
aheze Avatar answered Dec 07 '25 06:12

aheze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!