如何在标签的问题数组显示下一个问题

问题描述 投票:0回答:1

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喜欢的标签提出下一个问题数组中,并移动此前提出问题到数组的末尾。下面是当按钮被点击时调用,截至目前它呈现当按钮被点击同一个问题的功能我目前的银行代码:

         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
    }
}

}
swift loops if-statement int increment
1个回答
0
投票

你在你的nextQuestion方法,它复位计数每次设置y以0:

var y = 0

questionLbl.text = questions[y].question
questions.remove(at: y)
y += 1

所以,你总是给我的问题,数组中的第一个问题。另外,我不知道为什么你从问题阵列每次取出的第一个项目,因为你也各nextQuestion被调用时重建阵列。

© www.soinside.com 2019 - 2024. All rights reserved.