我有一個結構陣列,在單擊正確答案(下面的代碼)后,我想將陣列索引從 更改questions[0]為questions[1],我的意思是在陣列中移動。有沒有可能以任何簡單的方式?
任何解決方案都將被應用
var questions:[EasyQuestions] = [
EasyQuestions(question: "1 1", optionA: "5", optionB: "2", hint: "3-1", correctAnswer: 1),
EasyQuestions(question: "2 2", optionA: "4", optionB: "3", hint: "5-1", correctAnswer: 0)
].shuffled()
override func viewDidLoad() {
super.viewDidLoad()
configure(with: questions[0])
}
@IBAction func buttonClicked(_ sender: UIButton) {
if sender.tag == questions[0].correctAnswer {
print("The answer is correct") // after printing this I want to move on the next element of questions array
}
}
func configure(with question: EasyQuestions){
self.Question.text = question.question
self.button1.setTitle(question.optionA, for: .normal)
self.button2.setTitle(question.optionB, for: .normal)
}
uj5u.com熱心網友回復:
你可以試試
var currentIndex = 0
override func viewDidLoad() {
super.viewDidLoad()
configure()
}
@IBAction func buttonClicked(_ sender: UIButton) {
if sender.tag == questions[currentIndex].correctAnswer {
print("The answer is correct")
configure()
}
}
func configure(){
if currentIndex < questions.count {
let question = questions[currentIndex]
self.question.text = question.question
self.button1.setTitle(question.optionA, for: .normal)
self.button2.setTitle(question.optionB, for: .normal)
currentIndex = 1
}
}
確保將 button1 和 button2 的標簽分別設定為 0,1
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/359288.html
