I'm sure this is a basic question, but I'm new to this style of development, so thought Id ask...
I've worked through Apple's dice roller demo, so far so good - I'm using the code below to render and roll a single die;
struct DieView: View {
init(dieType: DieType) {
self.dieValue = Int.random(in: 1...dieType.rawValue)
self.dieType = dieType
}
@State private var dieValue: Int
@State private var dieType: DieType
var body: some View {
VStack {
if self.dieType == DieType.D6 {
Image(systemName: "die.face.\(dieValue)")
.resizable()
.frame(width: 100, height: 100)
.padding()
}
else {
Text("\(self.dieValue)")
.font(.largeTitle)
}
}
Button("Roll"){
withAnimation{
dieValue = Int.random(in: 1...dieType.rawValue)
}
}
.buttonStyle(.bordered)
Spacer()
}
}
Again, so far so good - works as I'd expect. I can now also add multiple DieViews to a DiceSetView and they display as I'd expect.
Where I'm stuck is in the DiceSetView, I want to both determine the total score across the dice, and also offer the ability to Roll All the dice in a set. (Ultimately I want another level above the set, so I'll be looking to roll all dice in all sets)
I can't simply call a func / method on the child view (i.e. iterate through them and sum their values, and roll each), I suspect I need to change how it's all structured, but not sure where to go from here...
Can anyone point me in the right direction?
Topic:
UI Frameworks
SubTopic:
SwiftUI