This is the whole code.
Thanks for showing the whole code. That helps me understand how your buttons are organized.
(Unfortunately, using many, many, super many global variables making the code hard to read, and difficult to maintain or modify. I hope the next step of your exercise would be something to remove all the global variables and re-define them in some appropriate places. But that is another issue.)
As far as I see your code, you have 8 buttons (embedded inside the subview Taste)
their symbols are modified, independently
their background are modified together to the same value
Why don't you simply make knopf1...knopf8 an Array?
struct ContentView: View {
// Placeholder variables before the real symbols are written to the buttons
@State private var knopf = Array(repeating: "questionmark", count: 8)
@State private var abdeckFarbe = Color.blue
var body: some View {
VStack {
Spacer()
// First row
HStack {
// The struct "Taste" below is called several times to draw the buttons.
ForEach(0..4) {i in
Taste(symbol: knopf[i])
.background(abdeckFarbe)
}
}
// Second row
HStack {
ForEach(4..8) {i in
Taste(symbol: knopf[i])
.background(abdeckFarbe)
}
}
Spacer()
// The cards are mixed.
Button(action: {
// Initialising the routine to re-mix the card with every tap on this button.
knopf = Array(repeating: "questionmark", count: 8)
bilder = ["moon", "moon", "house", "house", "sunset", "sunset", "cloud", "cloud"]
abdeckFarbe = Color.orange
knopf = bilder.shuffled()
//↓Are these really needed to be kept in global variables?
bilder = []
anzahl = 0
}, label: {
Text("Mischen")
})
}
}
}