0

In my code below, I am creating a quiz app. When the user clicks the button with "next", the question and the answer options update. When the user clicks an answer row, it highlights in green. When they click next, this answer row is no longer green, as the question and answer text has changed. However, with my current code, if the user wants to click that same answer row (the one that was previously green before clicking next but now has new answer text since next was clicked), they are being forced to click it twice to have the answer row turn green. So, after clicking next, it's taking two clicks to choose an answer instead of one. Any idea why? I beliv

import Foundation
import SwiftUI

var answerMatrix: [[Int]] = {
    var result = [[Int]]()
    for _ in 0..<4 {
        var row = [Int]()
        for _ in 0..<20 {
            row.append(0)
        }
        result.append(row)
    }
    return result
}()

var boolAnswerMatrix = Array(repeating: Array(repeating: false, count: 20), count: 4)





import SwiftUI
@available(iOS 15.0, *)
struct QuestionView: View {
    @StateObject var model = DataManager()
    @State var index = 0
    @State var length = 8
    @State var boolarray = [Bool](repeating: false, count: 4)
    
    var body: some View {
        
        let questionlist = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"]
        var currq = questionlist[index]
        VStack(spacing: 40) {
            VStack() {
                HStack {
                    Text("What's the Move?")
                        .lilacTitle()
                    
                    Spacer()
                    
                    Text("\(currq) out of \(length)")
                        .foregroundColor(Color("AccentColor"))
                        .fontWeight(.heavy)
                }
                .frame(maxWidth: 350, maxHeight: .infinity)
                .background(Color(red: 0.984313, green: 0.92941117647058824, blue:0.8470588235294118))
                
                ProgressBar(progress: CGFloat(350*(Int(currq)!)/length))
            }
            
            VStack(alignment: .leading) {
                if let qData = model.response?.record.questiondata.data {
                    
                    if let theAnswers = qData[currq] {
                        Text(theAnswers.question)
                            .frame(alignment: .leading)
                            .font(.system(size:20))
                            .foregroundColor(Color("AccentColor"))

                        VStack(spacing:20) {
                            if (theAnswers.answer1?.text) != nil {
                                AnswerRow(answer: Answer(logo: (theAnswers.answer1?.logo)!, text: (theAnswers.answer1?.text)!), ansind: index, ansnum: 1, selectinp: boolarray[0])
                            }
                            if (theAnswers.answer2?.text) != nil {
                                AnswerRow(answer: Answer(logo:  (theAnswers.answer2?.logo)!, text: (theAnswers.answer2?.text)!), ansind: index, ansnum: 2, selectinp: boolarray[1])
                            }
                            if (theAnswers.answer3?.text) != nil {
                                AnswerRow(answer: Answer(logo: (theAnswers.answer3?.logo)!, text: (theAnswers.answer3?.text)!), ansind: index, ansnum: 3, selectinp: boolarray[2])
                            }
                            if (theAnswers.answer4?.text) != nil{
                                AnswerRow(answer: Answer(logo: (theAnswers.answer4?.logo)!, text: (theAnswers.answer4?.text)!), ansind: index, ansnum: 4, selectinp: boolarray[3])
                            }
                        }
                    }
                    
                }
            }
            
            
            
            
            HStack(spacing: 0){
                if index > 0 {
                    Button(action: {
                        index -= 1
                        for i in 0..<self.boolarray.count {
                            self.boolarray[i] = false
                        }
                    }) {
                        Text("Back")
                            .font(.system(size:30))
                            .foregroundColor(.white)
                            .padding()
                            .background(
                                RoundedRectangle(cornerRadius: 20)
                                    .fill(Color(red: 0.50, green: 0.30, blue: 0.49))
                                    .shadow(radius: 10)
                            )
                    }
                    .padding()
                    .frame(maxWidth: .infinity, maxHeight: .infinity)
                    .background(Color(red: 0.984313, green: 0.92941117647058824, blue:0.8470588235294118))
                }
                if index == length - 1 {
                    NavigationLink {
                        ResultsView()
                    } label: {
                        Text("Get Results")
                            .font(.system(size:30))
                            .foregroundColor(.white)
                            .padding()
                            .background(
                                RoundedRectangle(cornerRadius: 20)
                                    .fill(Color(red: 0.50, green: 0.30, blue: 0.49))
                                    .shadow(radius: 10)
                            )
                        .padding()
                        .frame(maxWidth: .infinity, maxHeight: .infinity)
                        .background(Color(red: 0.984313, green: 0.92941117647058824, blue:0.8470588235294118))
                    }
                }
                else {
                    Button(action: {
                        index += 1
                    }) {
                        Text("Next")
                            .font(.system(size:30))
                            .foregroundColor(.white)
                            .padding()
                            .background(
                                RoundedRectangle(cornerRadius: 20)
                                    .fill(Color(red: 0.50, green: 0.30, blue: 0.49))
                                    .shadow(radius: 10)
                            )
                    }
                    .padding()
                    .frame(maxWidth: .infinity, maxHeight: .infinity)
                    .background(Color(red: 0.984313, green: 0.92941117647058824, blue:0.8470588235294118))
                }
            }
        }
        .task {
            await model.loadData()
        }
        .background(Color(red: 0.984313, green: 0.92941117647058824, blue:0.8470588235294118))
    }
}



import SwiftUI

struct AnswerRow: View {
    var answer: Answer
    var ansind: Int
    var ansnum: Int
    @State var selectinp: Bool
    @State private var isSelected = false
    @StateObject var model = DataManager()
    
    var body: some View {
        VStack {
            HStack(spacing: 20) {
                Image(systemName:(answer.logo))
                    .font(.system(size: 39))
                
                Text(answer.text)
                    .bold()
                
                
                if isSelected && boolAnswerMatrix[ansnum-1][ansind] {
                    Spacer()
                    Image(systemName: "checkmark.seal.fill")
                        .foregroundColor(.green)
                }
            }
            .padding()
            .frame(maxWidth: 350, alignment: .leading)
            .foregroundColor(boolAnswerMatrix[ansnum-1][ansind] ? Color(.green) : Color("AccentColor"))
            .background(Color(.white))
            .cornerRadius(10)
            .shadow(color: boolAnswerMatrix[ansnum-1][ansind] ? .green : .gray, radius: 5, x: 0.5, y: 0.5)
            .onTapGesture {
                isSelected = !isSelected
                setAnswerMatrixValue(row: ansnum, column: ansind, value: isSelected ? 1 : 0)
                boolAnswerMatrix[ansnum-1][ansind] = isSelected
            }
        }
    }
    func setAnswerMatrixValue(row: Int, column: Int, value: Int) {
        answerMatrix[row-1][column] = value
        boolAnswerMatrix[row-1][column] = (value == 1)
    }
}

M. Overby
  • 67
  • 6

0 Answers0