如何解决按下“下一步”按钮后问题不会更新
我正在开发一个测验应用程序,并且正在用下一组问题更新 UI。第一组加载得很好,但是当我试图通过单击“下一步”按钮来获取下一组问题时 - 什么也没发生。当我尝试调试时,我注意到不是更新问题,而是将问题添加到前一组问题中。请帮我弄清楚我做错了什么。
这是我的代码:
import Foundation
import Combine
final class QuizManager: ObservableObject {
@Published var quizQuestions: [QuizModel] = Bundle.main.decode("file.json")
var imageIndex = 0
var possibleAnswers = [String]()
var correctAnswers = 0
var questionsAsked = 0
init() {
getRandomQuestion()
}
func getRandomQuestion() {
imageIndex = Int.random(in: 0..<quizQuestions.count)
if quizQuestions[imageIndex].isCompleted {
imageIndex = Int.random(in: 0..<quizQuestions.count)
}
possibleAnswers.append(quizQuestions[imageIndex].description)
var index1 = Int.random(in: 0..<quizQuestions.count)
var index2 = Int.random(in: 0..<quizQuestions.count)
if index1 == imageIndex && index1 == index2 {
index1 = Int.random(in: 0..<quizQuestions.count)
} else {
possibleAnswers.append(quizQuestions[index1].description)
}
if index2 == imageIndex && index1 == index2 {
index2 = Int.random(in: 0..<quizQuestions.count)
} else {
possibleAnswers.append(quizQuestions[index2].description)
}
possibleAnswers.shuffle()
}
func checkAnswer(answer: String) -> Bool {
questionsAsked += 1
if quizQuestions[imageIndex].description == answer {
correctAnswers += 1
}
quizQuestions[imageIndex].isCompleted = true
return quizQuestions[imageIndex].description == answer
}
}
import SwiftUI
struct QuizQestionsView: View {
@Observedobject private var quizManager = QuizManager()
@State private var isCorrect = false
@State private var correctAnswer = 0
@State private var answerSelected = ""
@State private var isTapped = false
var body: some View {
ScrollView {
vstack {
ImageView(name: quizManager.quizQuestions[quizManager.imageIndex].image,contentMode: .scaleAspectFit,tintColor: .black)
.frame(minWidth: 150,idealWidth: 200,maxWidth: 250,minHeight: 150,idealHeight: 200,maxHeight: 250)
Spacer()
vstack {
ForEach(quizManager.possibleAnswers,id: \.self) { answer in
QuestionsView(answer: answer) {
self.isCorrect = self.quizManager.checkAnswer(answer: answer)
self.answerSelected = answer
self.isTapped = true
}
.disabled(isTapped)
.overlay(
RoundedRectangle(cornerRadius: 16.0)
.stroke(getColor(answer),linewidth: 1)
)
}
}
Spacer()
Button(action: {
self.quizManager.getRandomQuestion()
}) {
Text("NEXT")
}
}
}
}
func getColor(_ tag: String) -> Color {
if answerSelected == tag {
if isCorrect {
return Color.green
} else {
return Color.red
}
} else {
if isTapped && !isCorrect {
if tag == quizManager.quizQuestions[quizManager.imageIndex].description {
return Color.green
}
}
}
return Color.accentColor
}
问题视图如下所示:
var answer: String
var onTap: () -> Void
var body: some View {
Button(action: {
self.onTap()
}) {
Text(answer)
}
}
}
解决方法
在 getRandomQuestion()
中,正如您已经发现的那样,您的所有代码都附加到数组的末尾。
在函数开始时,您可以清除数组:
func getRandomQuestion() {
possibleAnswers = []
//the rest of the existing code here
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。