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.
onCommit. An action to perform when the user performs an action (for example, when the user presses the Return key) while the text field has focus.
onCommit. onCommit is an event handler/listener that handles when the user has finished his entry by pressing the Enter key. TextField( "Hint Text", text: $textInput, onCommit: { //code here } ).padding()
If you're supporting only iOS 15 and later, you can activate and dismiss the keyboard for a text field by focusing and unfocusing it. In its simplest form, this is done using the @FocusState property wrapper and the focusable() modifier – the first stores a Boolean that tracks whether the second is currently focused.
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:
print()
result doesn't show upLike 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()
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With