Reputation: 183
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
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