As we used UITextField in Swift
textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
And TextField in SwiftUI provide
TextField("", text: $input, onEditingChanged: { changed in
print("Changed")
self.output = "You are typing: " + self.input
}, onCommit: {
print("Commited")
self.output = "You typed: " + self.input
})
Changed
will print on begin edit and Commited
will print on return key press.
Now i m typing ABC
So now the question is
if i want to call any function or process on press of A, what steps i need to do for that ?
you can use this:
import SwiftUI
struct ContentView: View {
@State var txt = ""
var body: some View {
TextField("Enter txt", text: Binding<String>(
get: { self.txt },
set: {
self.txt = $0
self.callMe(with: $0)
}))
.padding(20)
}
func callMe(with tx: String) {
print("---> as you type \(tx)")
}
}
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