I'm currently using a SwiftUI View through UIHostingConfiguration as a UICollectionViewCell. Each cell is a trivia question, so I would like to disable all AnswerButton items once the user has selected one of them. I attempted to do so through adding @Binding var isDisabled: Bool to my AnswerButton and triggering .disabled(isDisabled == true) once the button is tapped. I'm passing this value as a @State var isDisabled: Bool = false from the QuestionCardView. During testing I noticed that the last QuestionCardView in the collection appears already disabled. Not sure which part of the process is causing the issue. Each QuestionCardView should be self reliant, so I'm unsure how the behavior is being passed over to the next cell. Any Advice would be appreciated!
QuestionCardView Code:
struct QuestionCardView: View {
var question: QuestionModel
@State var isDisabled: Bool = false
var body: some View {
HStack{
VStack {
Spacer()
Text(question.attributedTextFromHTML ?? "")
.lineLimit(nil)
.multilineTextAlignment(.center)
.foregroundColor(.white)
Spacer()
ForEach(question.allAnswers, id: \.self) {
AnswerButton(answer: $0, isDisabled: $isDisabled)
}
Spacer()
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.safeAreaPadding(.all)
.background(Color.gray.opacity(Configuration.opacity))
.cornerRadius(Configuration.cornerRadius)
}
}
AnswerButton Code:
struct AnswerButton: View {
var answer: Answer
@State private var didTap: Bool = false
@Binding var isDisabled: Bool
var body: some View {
Button(action: {
didTap = true
isDisabled = true
}) {
HStack(spacing: Configuration.spacing) {
Text(answer.text)
.foregroundColor(.white)
.contentShape(Capsule())
}
.frame(maxWidth: .infinity, maxHeight: Configuration.maxHeight)
.background(didTap ? (answer.isCorrect ? .green : .red) : .gray)
.clipShape(Capsule())
}
.disabled(isDisabled == true)
}
}
I tried moving the .disabled(isDisabled == true) to the QuestionCardView but it was less reliable. I'm expecting each card to appear with isDisabled set to false but the last card always appears disabled. When I apply breakpoints I sometimes receive an AttributeGraph: cycle detected through attribute warning, so that is most likely involved. The warning only happens when breakpoints are active though.