我有一个测验应用。我希望用户能够按下"下一个问题"按钮,并能够按顺序访问问题1,问题2,问题3等。
我希望应用程序有一个后退按钮,以便用户也可以访问以前的问题。例如,如果用户在问题3上,那么他们可以按后退按钮并访问问题2.
我正在考虑为问题结构中的每个问题分配一个数字,因此根据按下的按钮调用下一个编号的问题或前面编号的问题,即"下一个问题"按钮或"上一个问题"按钮。
这是我设置问题的方式:
struct Question {
var Question : String!
var Answers : String!
}
var Questions = [Question]()
var QNumber = Int()
@IBOutlet weak var labelForQuestion: UILabel!
@IBOutlet weak var textBoxForAnswer: UITextView!
override func viewDidLoad() {
//hiding answer
textBoxForAnswer.hidden = true
//Load Questions
Questions = [
Question(Question: "This is question 1", Answers: "This is answer 1"),
Question(Question: "This is question 2", Answers: "This is answer 2"),
Question(Question: "This is question 1", Answers: "This is answer 1"),
]
pickQuestion()
}
func pickQuestion() {
if Questions.count > 0 {
//setting Qnumber equal to 0 gives sequential quiz game no repeats
QNumber = 0
labelForQuestion.text = Questions[QNumber].Question
textBoxForAnswer.text = Questions[QNumber].Answers
//remove question so it doesnt come up again
Questions.removeAtIndex(QNumber)
}
}
@IBAction func Next(sender: AnyObject) {
pickQuestion()
}
@IBAction func showAnswer(sender: AnyObject) {
textBoxForAnswer.hidden = false
}
所以基本上我想让UIActions
能让我在问题之间向前和向后翻动。关于我将如何做到这一点的任何想法?
答案 0 :(得分:1)
您应该从QNumber = 0
中移除pickQuestion
并将其递增/递减到外面。此外,您可能应该删除Questions.removeAtIndex(QNumber)
。总的来说,改变应该是:
指定初始QNumber
值
var QNumber : Int = 0
更改pickQuestion
逻辑:
func pickQuestion() {
labelForQuestion.text = Questions[QNumber].Question
textBoxForAnswer.text = Questions[QNumber].Answers
}
更改Next
:
@IBAction func Next(sender: AnyObject) {
QNumber++; // you need some handling to not go out of bounds if you are already showing the last question
pickQuestion()
}
同样适用previous(...)
做QNumber--
。
补充说明:
next
,qNumber
,questions
,每个question
的成员等。