如何在一系列问题的标签中显示下一个问题
How to display next question in a label from an array of questions
Screenshot of current code in simulator, once the next button is clicked nothing changes and the question presented in the label remains the same
我正在构建一个调查应用程序并希望通过标签显示问题列表。但是,我当前的代码功能提出了相同的问题,点击按钮后我需要转到下一个问题。一旦点击按钮 id 就像标签一样显示数组中的下一个问题,并将先前提出的问题移动到数组的末尾。这是我当前的Swift点击按钮时调用的函数代码,截至目前,当点击按钮时它会提出相同的问题:
func nextQuestion() {
var questions = [choice1, choice2, choice3]
var y = 0
questionLbl.text = questions[y].question
questions.remove(at: y)
y += 1
var button = UIButton()
var tags = [0,1,2,3]
for quest in questions {
}
for idx in tags {
button = choiceButtons[idx]
if idx < options.count {
print("True! Index = \(idx) and options count = \(options.count)")
button.setTitle(options[idx], for: .normal)
}
else {
print("False! Index = \(idx) and options count = \(options.count)")
}
}
print(questions)
}
这是我的对象模型,其中包含用于问题数组的对象:
import Foundation
var choice1 = User.Choice(question: "How old are you?", opt1: "12", opt2: "11", opt3: "10", opt4: "23")
var choice2 = User.Choice(question: "What color are you?", opt1: "Black", opt2: "White", opt3: "Purple", opt4: "Brown")
var choice3 = User.Choice(question: "What day is it?", opt1: "Monday", opt2: "Tuesday", opt3: "Friday", opt4: "Sunday")
var users = [User]()
struct User {
var fields: Field
var choices = [Choice]()
struct Field {
var firstname: String
var lastname: String
var email: String
init(first: String, last: String, email: String) {
self.firstname = first
self.lastname = last
self.email = email
}
}
struct Choice {
var question: String
var opt1: String
var opt2: String
var opt3: String
var opt4: String
init(question:String, opt1: String, opt2: String, opt3: String, opt4: String) {
self.question = question
self.opt1 = opt1
self.opt2 = opt2
self.opt3 = opt3
self.opt4 = opt4
}
}
}
您在 nextQuestion 方法中将 y 设置为 0,每次都会重置计数:
var y = 0
questionLbl.text = questions[y].question
questions.remove(at: y)
y += 1
所以您总是得到问题数组中的第一个问题。另外,我不知道你为什么每次都从问题数组中删除第一项,因为每次调用 nextQuestion 时你也会重新创建数组。
Screenshot of current code in simulator, once the next button is clicked nothing changes and the question presented in the label remains the same
我正在构建一个调查应用程序并希望通过标签显示问题列表。但是,我当前的代码功能提出了相同的问题,点击按钮后我需要转到下一个问题。一旦点击按钮 id 就像标签一样显示数组中的下一个问题,并将先前提出的问题移动到数组的末尾。这是我当前的Swift点击按钮时调用的函数代码,截至目前,当点击按钮时它会提出相同的问题:
func nextQuestion() {
var questions = [choice1, choice2, choice3]
var y = 0
questionLbl.text = questions[y].question
questions.remove(at: y)
y += 1
var button = UIButton()
var tags = [0,1,2,3]
for quest in questions {
}
for idx in tags {
button = choiceButtons[idx]
if idx < options.count {
print("True! Index = \(idx) and options count = \(options.count)")
button.setTitle(options[idx], for: .normal)
}
else {
print("False! Index = \(idx) and options count = \(options.count)")
}
}
print(questions)
}
这是我的对象模型,其中包含用于问题数组的对象:
import Foundation
var choice1 = User.Choice(question: "How old are you?", opt1: "12", opt2: "11", opt3: "10", opt4: "23")
var choice2 = User.Choice(question: "What color are you?", opt1: "Black", opt2: "White", opt3: "Purple", opt4: "Brown")
var choice3 = User.Choice(question: "What day is it?", opt1: "Monday", opt2: "Tuesday", opt3: "Friday", opt4: "Sunday")
var users = [User]()
struct User {
var fields: Field
var choices = [Choice]()
struct Field {
var firstname: String
var lastname: String
var email: String
init(first: String, last: String, email: String) {
self.firstname = first
self.lastname = last
self.email = email
}
}
struct Choice {
var question: String
var opt1: String
var opt2: String
var opt3: String
var opt4: String
init(question:String, opt1: String, opt2: String, opt3: String, opt4: String) {
self.question = question
self.opt1 = opt1
self.opt2 = opt2
self.opt3 = opt3
self.opt4 = opt4
}
}
}
您在 nextQuestion 方法中将 y 设置为 0,每次都会重置计数:
var y = 0
questionLbl.text = questions[y].question
questions.remove(at: y)
y += 1
所以您总是得到问题数组中的第一个问题。另外,我不知道你为什么每次都从问题数组中删除第一项,因为每次调用 nextQuestion 时你也会重新创建数组。