TechnologiesSwiftUI

SubmitTriggers struct

iOSmacOStvOSwatchOSvisionOSiOS 15.0+✓ renders

A type that defines various triggers that result in the firing of a

How it works

SubmitTriggers is an option set that names the kinds of user actions SwiftUI treats as a submission — most commonly pressing Return in a text field, but also confirming a search field. You pass it to the onSubmit(of:) modifier to declare which submit-capable controls in a view hierarchy should run your handler, and you pass it to submitScope(_:) to stop a submission from propagating past a boundary. Reach for it whenever you want a control's natural "commit" gesture to drive your code, rather than wiring up a separate button or watching for individual keystrokes.

  1. Choose a trigger with the .text and .search options

    SubmitTriggers conforms to OptionSet, so each value is one or more submission kinds. The .text option matches the Return key in controls like TextField and SecureField, while .search matches the submit gesture of a searchable field; you can combine them with array syntax ([.text, .search]). The example asks for .text so that pressing Return in the TextField counts as a submission.

  2. React to submissions with onSubmit(of:)

    The onSubmit(of:) modifier takes a SubmitTriggers value and a closure, and runs that closure when a matching control inside the modified hierarchy is submitted. Here .onSubmit(of: .text) attaches to the TextField and updates greeting from the current name the moment the user presses Return — no submit button required.

  3. Scope the trigger to the controls you mean

    Because onSubmit(of:) applies to every matching control in its subtree, place it where it should take effect. In the example it sits directly on the TextField, so only that field's Return key fires the handler; attaching it higher in the VStack would catch submissions from any text control nested below.

  4. Stop propagation with submitScope(_:)

    Submissions normally bubble up to the nearest matching onSubmit(of:). To prevent that for a particular subtree — for instance an inner field that should not trigger an outer form's submit handler — wrap it in submitScope(_:) with the SubmitTriggers you want blocked. The single field in this demo needs no scoping, but the same .text value is what you would pass to contain it.

Try it — Change .onSubmit(of: .text) to .onSubmit(of: .search) and watch the handler stop firing on Return, since the TextField no longer matches the requested trigger.

Example & preview

Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.

SubmitTriggers.swift
struct SubmitTriggersDemo: View {
    @State private var name = ""
    @State private var greeting = "Type a name and press return"

    var body: some View {
        VStack(spacing: 16) {
            TextField("Name", text: $name)
                .textFieldStyle(.roundedBorder)
                .onSubmit(of: .text) {
                    greeting = name.isEmpty ? "Hello!" : "Hello, \(name)!"
                }
            Text(greeting)
                .font(.headline)
        }
        .padding()
    }
}
Live preview
Name Type a name and press return
Name Type a name and press return
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →