4

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 ?

1 Answer 1

5

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)")
}
}
Sign up to request clarification or add additional context in comments.

1 Comment

glad I could help. Please mark the answer as correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.