This is so easy to do in UIKit, struggling with SwiftUI. I have in my ContentView a series of custom buttons. Each has their own @State variable which governs whether are toggled on or off - and update accordingly.
struct AudioToggleButton: View, Hashable {
// Some Details omitted here for brevity
@State private var toggled = false
var body: some View {
Button(action: {
toggled.toggle()
}) {
Image(systemName: iconString)
.frame(width: 70, height: 70)
.foregroundColor(self.toggled == true ? .white : Color(UIColor.lightGray))
.background(self.toggled == true ? .black : .white)
.font(.title)
.clipShape(Circle())
.overlay(Circle()
.stroke(self.toggled == true ? Color(UIColor.darkGray) : Color(UIColor.lightGray), lineWidth: 6))
.padding(4)
}
}
I have in my ContentView where they are placed (a bunch). I have a regular vanilla button in my ContentView where I want to turn off all the buttons (the toggle) at once. I can change each by having them in an array & looping over them - and having a func in the custom button's struct that sets the @state variable - but the UI never updates to change because of the var change.
How can I turn off (toggle) any buttons that are toggled on from outside the buttons?
self.toggled ? .black : .white