I'm searching for a way to simplify/refactor the addition of .onChange(of:) in a SwiftUI view that has MANY TextFields. If a solution were concise, I would also move the modifier closer to the appropriate field rather than at the end of, say, a ScrollView. In this case, all of the .onChange modifiers call the same function.
Example:
.onChange(of: patientDetailVM.pubFirstName) { x in
changeBackButton()
}
.onChange(of: patientDetailVM.pubLastName) { x in
changeBackButton()
}
// ten+ more times for other fields
I tried "oring" the fields. This does not work:
.onChange(of:
patientDetailVM.pubFirstName ||
patientDetailVM.pubLastName
) { x in
changeBackButton()
}
This is the simple function that I want to call:
func changeBackButton() {
withAnimation {
showBackButton = false
isEditing = true
}
}
Any guidance would be appreciated. Xcode 13.2.1 iOS 15
Why not just use a computed var?
@State private var something: Int = 1
@State private var another: Bool = true
@State private var yetAnother: String = "whatever"
var anyOfMultiple: [String] {[
something.description,
another.description,
yetAnother
]}
var body: some View {
VStack {
//
}
.onChange(of: anyOfMultiple) { _ in
//
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With