非更新视图

mnemlml8  于 2022-10-04  发布在  Swift
关注(0)|答案(0)|浏览(83)

总的来说,我对Swift和编程相当陌生,但我正在尝试构建一种有效的测验视图,它从JSON文件中获取测验问题,并使用自定义的“答案”视图以LazyVGrid模式将它们显示在屏幕上。

这是我的自定义答案视图(很抱歉测试到处都是这些名字,我有重复的文件来尝试不同的事情,而不会干扰我的原始文件!):

struct TestAnswerTest: View {
    @State var answer: String
    @State private var selected = false
    var correctAnswers: [String]

    var body: some View {
        Text(answer)
            .font(.title)
            .bold()
            .frame(width: 165, height: 140)
            .contentShape(RoundedRectangle(cornerRadius: 25))
            .onTapGesture {
                self.selected.toggle()
            }
    }
}

我的问题结构:

struct Question2: Codable, Identifiable {
    let id: Int
    let category: String
    let question: String
    let answers: [String]
    let correctAnswers: [String]
    let options: Int
}

以下是我的json数据示例以供参考:

{
    "id": 1,
    "category": "staff",
    "question": "What are the 2 common names for the lines and spaces on sheet music?n(Select 2 answers)",
    "answers": ["Staff", "Stave", "String", "Stem", "Stake", "Store"],
    "correctAnswers": ["Staff", "Stave"],
    "options": 6
}

这是我的测验展示视图:

struct StaffRecap2: View {
    @State private var questionNumber = 0
    private let twoColumnGrid = [GridItem(.flexible()), GridItem(.flexible())]
    let questions:[Question2] = Bundle.main.decode("questions.json")

    var body: some View {
        let staffQuestions = questions.filter { B1a3a1b.category.elementsEqual("staff")}

        NavigationView {
            VStack{
                Text(staffQuestions[questionNumber].question)

                LazyVGrid(columns: twoColumnGrid) {
                    ForEach(0..<staffQuestions[questionNumber].options, id:.self) { number in
                        TestAnswerTest(answer: staffQuestions[questionNumber].answers[number],
                                       correctAnswers: staffQuestions[questionNumber].correctAnswers)
                    }
                    .toolbar {
                        ToolbarItemGroup(placement: .bottomBar) {
                            Button {
                                questionNumber += 1
                                print(staffQuestions[questionNumber])
                            } label: {
                                Text("Next Question")
                            }
                            .disabled(questionNumber == staffQuestions.count)
                        }
                    }
                }
            }
            .frame(maxWidth: .infinity, maxHeight: .infinity)
            .background(Gradient(colors: [.teal, .blue]))
            .navigationTitle("Staff Recap")
            .navigationBarTitleDisplayMode(.inline)
            .toolbarBackground(.orange, for: .navigationBar)
            .toolbarBackground(.visible, for: .navigationBar)
        }
    }
}

我的问题是,如果我手动更改“questonNumber”属性和带有所有正确细节的模拟器和预览中的代码,似乎一切都很好,但如果我使用按钮转到下一个问题,它会正确地更改问题,但不会将任何答案视图更新为新的答案选项。我真的不明白为什么当我在Answer视图中使用onTapGesture修饰符打印正确的Answers数组时,它也会为问题打印正确的数组,并在我按下按钮时正确地更改。

我有点困惑,我相信这可能是我遗漏了一些简单的东西,但我陷入了僵局,如果有人帮助或指出我做错了什么,我将不胜感激

提前谢谢!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题