TextField onCommit is never called

Your code should work fine, as it does on Xcode 11.0 beta 1. However, I can confirm this does not currently work as expected on beta 2. I filled a bug report to Apple for this issue.


Update: This issue was fixed with Xcode 11 beta 3.


Try it in the real simulator.

Keep some notes in mind:

  • Unfortunately debugger is not working with live preview
  • Console is not connected to the live preview, so print() result doesn't show up
  • You can change your code a bit and build a simple log console to see the result if you want to test it in preview mode:

Like this:

struct ContentView : View {

    @State private var log: String = "Logs: "
    @State var value: String = ""

    var body: some View {
        VStack() {

            // Logger
            Text(log)
                .lineLimit(0)
            .padding()
            //
            Spacer()

        TextField($value,
                  placeholder: Text("placeholder"),
                  onEditingChanged: { edit in
                    self.log.append("\n edit = \(edit)")
        },
                  onCommit: {
                    self.log.append("\n COMITTED!")
        }).textFieldStyle(.roundedBorder).padding(.horizontal, 20)
            Spacer()
        }
    }
}
  • Live preview has a bug that cause it to not work properly with keyboard and it has an epic delay (mine was about 15 minutes) to show the onscreen keyboard. So if you realy want to see the result in live preview, you should be very patient and wait for the keyboard:

enter image description here

  • Use simulator or real device to test what you want. Video: Xcode11-Beta1

enter image description here