单击按钮后从数组索引 0 移动到 1 等等
After button click move from array index 0 to 1 and so on
我有一个结构数组,在单击正确答案(下面的代码)后,我想将数组索引从 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)
}
你可以试试
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
}
}
make sure to set tags of button1 and button2 to 0,1 respectively
我有一个结构数组,在单击正确答案(下面的代码)后,我想将数组索引从 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)
}
你可以试试
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
}
}
make sure to set tags of button1 and button2 to 0,1 respectively