10

I have been working with SwiftUI for a week now, and as everyone, I am struggling to identify if something that doesn't work is either a bug or a misunderstanding on my part.

I am using the TextField view but the onCommit closure is never executed. The onEditingChanged works fine. It gets called with true when the text field gains focus, and it gets called with false when it looses it.

The onCommit closure, however, will never execute. According to the little documentation that is available in the code:

onCommit: The action to perform when the user performs an action (usually the return key) while the TextField has focus.

This is the code:

TextField($value,
          placeholder: Text(placeholder),
          onEditingChanged: { edit in
            print("edit = \(edit)")
          },
          onCommit: {
            print("COMITTED!")
          }).textFieldStyle(.roundedBorder).padding(.horizontal, 20)

Ideally, I would like to move the focus from the text field receiving the RETURN key and put the focus on the following field. It's easy with UITextField and the resignFirstResponder() and becomeFirstResponder().

I already managed to use the UIViewRepresentable to embed an old UITextField, but that's not the point of this post. I would really hope to make it work in pure SwiftUI code.

If I can ever get the onCommit to work, what should I put there to achieve my goal?

Update: It seems the problem is only present with iOS, not macOS where it seems to work fine.

Update 2: This is a video of the result of running the suggested code on my machine. I did file a bug with Apple... we'll see.

enter image description here

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
kontiki
  • 37,663
  • 13
  • 111
  • 125

2 Answers2

4

Try it in the real simulator.

Keep some notes in mind:

  • Unfortunately, debugger is not working with the live preview
  • Console is not connected to the live preview, so print() result doesn't show up (Until Xcode 14)
  • 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

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
  • Thanks a lot for your example! There is something extremely odd. I still cannot make it work... I created an empty project and pasted the ContentView you provided. Still no luck. onCommit never gets called. Now that I know it works for someone, I am positive it is not a coding issue... I'll look somewhere else (don't know where yet ;-). By the way, I always test on the simulator. The live preview cannot be trusted. Too many things are broken there and I kept testing everything twice to rule out a preview problem... so I finally decided to discard it until it gets more stable. – kontiki Jun 18 '19 at 16:18
  • The default project that Xcode creates, with the ContentView you provided, does not work for me. After typing something and hitting RETURN, COMMITTED is not logged to the screen as it did in your screenshot. – kontiki Jun 18 '19 at 19:36
  • Witch version of Xcode you are on? It's build on Xcode 11 beta-1. Did you see `edit = true` when you select the `textField`. Did keyboard comes up? – Mojtaba Hosseini Jun 18 '19 at 19:39
  • Xcode 11.0 beta 2 (11M337n). Downloaded it yesterday. Yes, edit is printed fine. I will perform a clean install tomorrow, in another computer tomorrow, and see if the problem is still there. – kontiki Jun 18 '19 at 20:27
  • Yes, the keyboard shows up. Both hitting the actual ENTER key, or tapping on the virtual keyboard has the same effect. – kontiki Jun 18 '19 at 20:30
  • Thanks let me know if it persist. I guess beta-2 has significant changes. I heard so much folks saying some codes not working. Please fill a bug report to apple if you find something strange like this. – Mojtaba Hosseini Jun 18 '19 at 20:30
  • I updated the answer to include a video of how your code works on my machine. Thank you! Already filed a bug report. – kontiki Jun 18 '19 at 20:49
  • 1
    Hmm. Why keyboard closes by the way? I'm going to update my macOS -> Xcode -> iOS and then recheck for workaround this. But its absolutely a proven bug. – Mojtaba Hosseini Jun 18 '19 at 20:58
  • I don't know why it closes, I assumed that was normal. In your case it doesn't then, I take it. – kontiki Jun 18 '19 at 21:12
4

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.

M Reza
  • 18,350
  • 14
  • 66
  • 71