Checking for delete entry.
With the UIKit shouldChangeCharactersIn... delegate method the backspace is the equivalent of an empty string.
In the SwiftUI TextField .onChange() block it is necessary to use a capture list for the @State property being updated and do a comparison between that and the new value in the closure.
@State var textInput: String = ""
var body: some View {
TextField("numerical entries only", text: $textInput)
.onChange(of: textInput) { [textInput] newValue in
if newValue.count < textInput.count { print("- backspace detected") }
}
...
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: