The onCommit
callback gets called when user taps return.
The onEditingChanged
is a little bit more tricky and gets called when user taps on the TextField or taps return. The changed value is set to true when user taps on the TextField and it’s set to false when user taps return.
Example 1:
struct ContentView: View {
@State var username: String = ""
var body: some View {
VStack(alignment: .leading) {
TextField("Enter username...", text: $username)
Text("Your username: \(username)")
}.padding()
}
}
Example 2:
struct ContentView: View {
@State var username: String = ""
var body: some View {
VStack(alignment: .leading) {
TextField("Enter username...",
text: $username,
onEditingChanged: { (changed) in
print("Username onEditingChanged - \(changed)")
}) {
print("Username onCommit")
}
Text("Your username: \(username)")
}.padding()
}
}