4

I'm trying to change value of Text while typing in TextField. When i type anything in TextField, i want to do some calculation with Textfield value and display it on Text. In Swift, we have func textFieldDidChange(_ textField: UITextField) delegate method of UITextField to track that UITextField is changed.

In SwiftUI, i tried to create TextField with onEditingChanged. but somehow it is not going into that block. I don't know if i am doing anything wrong with it or not.

    @State var totalAmount: String = ""
    @State var addAmount: String = ""
    var body: some View {

            HStack {
                TextField("0.0", text: $totalAmount, onEditingChanged: { _ in
                    self.addAmount = //doing some calculation with totalAmount
                })
                Text(self.addAmount)

            }
    }

Can anyone help with this?

Anjali Kevadiya
  • 3,567
  • 4
  • 22
  • 43
  • 1
    `onEditingChanged` doesn't fire when the $totalAmount binding changes. It fires when your View goes in to Edit mode. There's no equivalent to `textFieldDidChange` in SwiftUI but there are ways to use a custom binding to act on text changes (I can't locate the SO answer right now but there is one). – KB-YYZ Sep 27 '19 at 20:43
  • I know onEditingChanged will not works. but this is what i have tried. Are you talking about this? https://stackoverflow.com/questions/57875550/textfield-changes-in-swiftui – Anjali Kevadiya Sep 27 '19 at 21:01

1 Answers1

10

SwiftUI is not like Cocoa. It is not event-driven, and there is no communication from one interface object to another. There is only data. Data flows thru state variables. It flows up by a state variable's binding and down by a state variable's value.

So you don't need an onEditingChanged event, or any other event. $totalAmount is a binding. It changes when the text field's text changes, automatically.

Moreover, totalAmount is a state variable. If the Text uses this as its value, directly or indirectly, the text changes automatically when totalAmount changes.

So just use a calculated variable that depends on totalAmount and make that the value of the Text and you're done.

Extremely basic example:

struct ContentView : View {
    @State var totalAmount = ""
    let addAmount = 10.0 // or whatever
    var calc : Double { (Double(totalAmount) ?? 0.0) + addAmount } // or whatever
    var body: some View {
        HStack {
            TextField("Type a number", text: $totalAmount)
                .textFieldStyle(RoundedBorderTextFieldStyle())
                .frame(width:100)
            Text(String(self.calc))
        }
    }
}
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I know onEditingChanged is not gonna work but i tried that way also. I was doing the same thing but i was calculating value through one function inside ContentView. When i accessed that function xcode shows up error somewhere else and that code was working fine. Anyways its working perfect now! Thank you so much!! – Anjali Kevadiya Sep 27 '19 at 21:21