我正在构建调查应用程序,并希望通过标签显示问题列表。但是,我当前的代码功能提出了相同的问题,一旦点击一个按钮,我就需要转到下一个问题。轻按按钮后,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
}
}
}
答案 0 :(得分:0)
您要在nextQuestion方法中将y设置为0,该方法每次都会重置计数:
var y = 0
questionLbl.text = questions[y].question
questions.remove(at: y)
y += 1
因此,您总是在问题数组中得到第一个问题。另外,我也不知道为什么每次都要从问题数组中删除第一项,因为每次调用nextQuestion时也会重新创建数组。