I am not sure whether I expressed the problem clear enough. I need to change the attributes of the buttons from any place in my code; from "far away" so to say. I know that I can set the attributes of texts or buttons by e.g.
.background(Color.green)
but I these attributes are always right below those elements. What I need is to set an attribute of a button or whatever when I click on another button (let's say a "reset button"). When I do it with a
@State private var
then I change it for all at the same time but I need to call each button separately.
Look at this code (it is not the real program but only an example):
var body: some View {
VStack {
Text("This is Text 1")
.foregroundColor(Color.green)
Button(action: {
print("Blabla")
}, label: {
Image(systemName: "sunset")
.font(.system(size: 100))
.background(Color.blue)
})
.disabled(false)
// This button shall change the color and the ".disable" of the button above and the color of the text, too.
Button(action: {
// Code to change the attributes goes here but what is it?
}, label: {
Text("Reset")
})
}
}
I am afraid that I can solve this dilemma only with several
@State private var
and have to write as many subprograms as I need buttons instead to write a
var body: some View {
HStack {
prototypeButton(Text: "Button 1")
prototypeButton(Text: "Button 2")
}
}
struct prototypeButton: View {
var body: some View {
Button(action: {},
label: {
Text("Button")
}
}
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: