Newbie here. I am creating a quiz app. Unlike most of the examples, every question is in a separate view. I'd like to keep the score and show it at the end.
Example:
struct questionOne: View {
@State var isSelected = false
var body: some View {
GeometryReader { geometryProxy in
VStack(alignment: .center) {
TRpic().cornerRadius(10)
Text("What's the capital of Turkey?")
.font(.title)
Spacer()
Button(action: {self.isSelected.toggle()}) {
Text("Istanbul")
} .buttonStyle(SelectedButtonStyleFalse(isSelected: self.$isSelected))
Button(action: {self.isSelected.toggle()}) {
Text("Ankara")
}.buttonStyle(SelectedButtonStyle(isSelected: self.$isSelected))
Button(action: {self.isSelected.toggle()}) {
Text("Athens")
} .buttonStyle(SelectedButtonStyleFalse(isSelected: self.$isSelected))
Spacer()
NavigationLink(destination: questionTwo()) {
VStack {
Text("Next Question")
Adview().frame(width: 150, height: 50)
}
}
}
}
}
}
struct SelectedButtonStyle: ButtonStyle {
@Binding var isSelected: Bool
public func makeBody(configuration: Self.Configuration) -> some View {
configuration.label
.padding(20)
.foregroundColor(.white)
.background(isSelected ? Color.green : Color.gray)
.cornerRadius(10.0)
}
}
struct SelectedButtonStyleFalse: ButtonStyle {
@Binding var isSelected: Bool
public func makeBody(configuration: Self.Configuration) -> some View {
configuration.label
.padding(20)
.foregroundColor(.white)
.background(isSelected ? Color.red : Color.gray)
.cornerRadius(10.0)
}
}
I have two button styles: SelectedButtonStyle (for the true answer) and SelectedButtonStyleFalse (for the false answer)
All 50 questions (views) are connected via NavigationView. And in the end, I want to show the total score in a separate view. For example "You have scored 35/50".
Thanks!