I am building a survey app and want to display a list of questions through a label. However, my current code functionality presents the same question and I need to go to the next question once a button is tapped. Once the button is tapped id like for the label to present the next question in the array and also move the previously asked question to the end of the array. Here is my current Swift code for the function that is called when the button is tapped, as of now it presents the same question when the button is tapped:
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)
}
Here's my object model that contains the objects that are used for the array of 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
}
}
}
Aucun commentaire:
Enregistrer un commentaire