I have a ForEach that displays 70 buttons:
import SwiftUI
struct BoxView: View {
@ObservedObject var gameLogic: GameLogic
var body: some View {
VStack {
ForEach(0..<10) { number in
HStack {
ForEach(0..<7) { number in
ButtonView(gameLogic: GameLogic())
.frame(width: 30, height: 30)
}
}
}
}
}
}
Each view includes another view – ButtonView:
import SwiftUI
struct ButtonView: View {
@ObservedObject var gameLogic: GameLogic
var body: some View {
Button(action: { gameLogic.displayLetter(whichBox: 0) }) {
Text(gameLogic.boxLabel[0])
.foregroundColor(.black)
.font(.title)
.frame(width: 30, height: 30)
.border(gameLogic.boxOutline[0])
}
.disabled(gameLogic.letterBoxDisabled[0])
}
}
There are three arrays and a function within the ButtonView struct. Each instance of the button needs to have a different array element number and pass a different number in the function – from 0 to 69. I’m not sure if this is possible. The only thing I could think to try was to create a variable in gameLogic and increment it by 1 in each iteration of the button. However, I wasn’t able to add code to increment that number in the BoxView struct. I get: “Type ‘()’ cannot conform to ‘View’.”
I’m sure there’s a way to do this but, being new to Swift and SwiftUI, I don’t know what it is. Thanks.