WilliamD47
WilliamD47

Reputation: 183

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

Upvotes: 3

Views: 598

Answers (1)

aheze
aheze

Reputation: 30318

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
        }
    }
}

Upvotes: 3

Related Questions