[英]How do I assign a number to each member of a structure? (swift)
我有一個測驗應用程序。 我希望用戶能夠按“下一個問題”按鈕並能夠按順序訪問問題 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
使我能夠在問題之間向前和向后滑動。 關於我將如何做到這一點的任何想法?
您應該從pickQuestion
刪除QNumber = 0
並在外面增加/減少它。 此外,您可能應該刪除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
的成員等。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.