This is the whole code. It's not secret but only an exercise for me. It is not finished yet because I am stuck.
I did not know that this problem was so difficult. I have several buttons (it could also be texts) and want to change their attributes from another place of my code, e.g. a reset button. Look at the code. The reset button is the button "Mischen". I have to cut this post into halves because it exceeds the character limit.
import SwiftUI
var farbe = Color.accentColor
// The Array for the symbols of the buttons
var bilder = ["moon", "moon", "house", "house", "sunset", "sunset", "cloud", "cloud"]
var arrayAuswahl = 0
var anzahl = 0
var bildAuswahl = ""
var rateBild1 = ""
var rateBild2 = ""
var index = 0
var sperrung = 0
var knopfIDallgemein = ""
var knopfID1 = ""
struct ContentView: View {
// Placeholder variables before the real symbols are written to the buttons
@State private var knopf1 = "questionmark"
@State private var knopf2 = "questionmark"
@State private var knopf3 = "questionmark"
@State private var knopf4 = "questionmark"
@State private var knopf5 = "questionmark"
@State private var knopf6 = "questionmark"
@State private var knopf7 = "questionmark"
@State private var knopf8 = "questionmark"
@State private var abdeckFarbe = Color.blue
/// Start. Only for testing. Maybe obsolete later
@State private var knopfSperren = false
/// End
var body: some View {
VStack {
Spacer()
// First row
HStack {
// The struct "Taste" below is called several times to draw the buttons.
Taste(symbol: knopf1)
.background(abdeckFarbe)
Taste(symbol: knopf2)
.background(abdeckFarbe)
Taste(symbol: knopf3)
.background(abdeckFarbe)
Taste(symbol: knopf4)
.background(abdeckFarbe)
}
// Second row
HStack {
Taste(symbol: knopf5)
.background(abdeckFarbe)
Taste(symbol: knopf6)
.background(abdeckFarbe)
Taste(symbol: knopf7)
.background(abdeckFarbe)
Taste(symbol: knopf8)
.background(abdeckFarbe)
}
Spacer()
// The cards are mixed.
Button(action: {
// Initialising the routine to re-mix the card with every tap on this button.
knopf1 = "questionmark"
knopf2 = "questionmark"
knopf3 = "questionmark"
knopf4 = "questionmark"
knopf5 = "questionmark"
knopf6 = "questionmark"
knopf7 = "questionmark"
knopf8 = "questionmark"
bilder = ["moon", "moon", "house", "house", "sunset", "sunset", "cloud", "cloud"]
abdeckFarbe = Color.orange
// The number of elements in the array "bilder" is read.
anzahl = bilder.count
// The subroutine is executed as long as there are still elements in the array.
if anzahl 0 {
// The loop is executed as long as there are buttons.
for maxAnzahlKnoepfe in 0...7 {
// Pick a random symbol name from the array.
arrayAuswahl = Int.random(in: 0...anzahl-1)
// The name of the element is written to the variable "bildAuswahl"
bildAuswahl = bilder[arrayAuswahl]
// The picked element is removed from the array to prevent that it is picked again.
bilder.remove(at: arrayAuswahl)
anzahl = bilder.count
// Depending on the counter "maxAnzahlKnoepfe" the corresponding button gets a symbol.
if maxAnzahlKnoepfe == 0 {
knopf1 = bildAuswahl}
else if maxAnzahlKnoepfe == 1 {
knopf2 = bildAuswahl}
else if maxAnzahlKnoepfe == 2 {
knopf3 = bildAuswahl}
else if maxAnzahlKnoepfe == 3 {
knopf4 = bildAuswahl}
else if maxAnzahlKnoepfe == 4 {
knopf5 = bildAuswahl}
else if maxAnzahlKnoepfe == 5 {
knopf6 = bildAuswahl}
else if maxAnzahlKnoepfe == 6 {
knopf7 = bildAuswahl}
else if maxAnzahlKnoepfe == 7 {
knopf8 = bildAuswahl}
}
}
}, label: {
Text("Mischen")
})
}
}
}