How to use textChange event in SwiftUI TextField Component? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to use textChange event in SwiftUI TextField Component? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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 ?

Answer

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)")
}
}
We are here to answer your question about How to use textChange event in SwiftUI TextField Component? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji