1

I want to get notified when the user is done writing text in a TextField in SwiftUI.

The senario in as follows:

-the user enters text in a TextField.

-when the user wrote a string that is longer then 3 characters and paused or finished writing I want the app to get notified so I can work with that String.

tried .onEditingChanged but it will call the action with each change and not only when the user finished editing.

TheMachineX
  • 179
  • 1
  • 11
  • Please check if this helps https://stackoverflow.com/questions/56550881/textfielddidbeginediting-and-textfielddidendediting-in-swiftui – Subha_26 Jul 12 '20 at 13:45

1 Answers1

0

Try below code, this way you can check the real time value entered by user in a textfield.

   struct MyFormView: View {
    @State var typingText: String = "" {
        willSet {
            if typingText.count == 3 {
                print("User now entering 4th character")
            }
        }
        didSet {
            print(typingText)
        }
    }

    var body: some View {
        let bindText = Binding<String>(
            get:{self.typingText},
            set:{self.typingText = $0})
    return VStack(alignment: .leading, spacing: 5) {
        Text("Name")
        TextField("Enter Name", text: bindText)
            .keyboardType(.default)
            .frame(height: 44)
        }.padding(.all, CGFloat(10))
    }
}
Sona
  • 394
  • 3
  • 15