Swift 问题不会在“之后更新”;“下一步”;按钮按下了
我正在开发一个测验应用程序,并用下一组问题更新UI。第一个问题集正在加载,但当我试图通过点击“下一步”按钮获取下一个问题集时,什么都没有发生。当我尝试调试时,我注意到这些问题并没有更新,而是添加到了前一组问题中。请帮我找出我做错了什么 这是我的密码:Swift 问题不会在“之后更新”;“下一步”;按钮按下了,swift,foreach,swiftui,Swift,Foreach,Swiftui,我正在开发一个测验应用程序,并用下一组问题更新UI。第一个问题集正在加载,但当我试图通过点击“下一步”按钮获取下一个问题集时,什么都没有发生。当我尝试调试时,我注意到这些问题并没有更新,而是添加到了前一组问题中。请帮我找出我做错了什么 这是我的密码: import Foundation import Combine final class QuizManager: ObservableObject { @Published var quizQuestions: [QuizModel]
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
}
在getRandomQuestion()
中,如您所知,所有代码都会附加到数组的末尾
在函数开始时,可以清除数组:
func getRandomQuestion() {
possibleAnswers = []
//the rest of the existing code here
}
由于某些原因,“下一步”按钮没有响应
可能的回答可能也应该是@Published
是!它起作用了。非常感谢。
func getRandomQuestion() {
possibleAnswers = []
//the rest of the existing code here
}