我按照了一个测验教程,一切正常,但是当我想添加重置按钮以便测验可以重新开始时它不起作用。这是代码,我试图重置数字,我尝试清空数组,但它没有工作。请帮助。
import UIKit
struct Question {
var Question : String!
var Answers : [String]!
var Answer : Int!
}
class ViewController: UIViewController {
@IBOutlet var buttons: [UIButton]!
@IBOutlet weak var qLabel: UILabel!
@IBOutlet weak var novceki: UILabel!
var Questions = [Question]()
var Qnumber = Int()
var AnswerNumber = Int()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
Questions = [Question(Question: "Jesi pojeo?" , Answers: ["Jesam", "Nisam", "Budem", "Fuj"], Answer: 0),
Question(Question: "Najvisi vrh piramide?" , Answers: ["10", "39", "40", "55"], Answer: 2),
Question(Question: "Jesi popusio?" , Answers: ["Jesam", "Nisam", "Budem", "Fuj"], Answer: 0),
Question(Question: "Jesi bio vani?" , Answers: ["Jesam", "Nisam", "Budem", "Fuj"], Answer: 3)]
pickQuestion()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func pickQuestion (){
if Questions.count > 0{
//Qnumber = 0
Qnumber = random() % Questions.count
qLabel.text = Questions[Qnumber].Question
AnswerNumber = Questions[Qnumber].Answer
for i in 0..<buttons.count{
buttons[i].setTitle(Questions[Qnumber].Answers[i], forState: UIControlState.Normal)
}
Questions.removeAtIndex(Qnumber)
}
else {
NSLog("Done!")
}
}
@IBAction func Btn1(sender: UIButton) {
if AnswerNumber == 0 {
pickQuestion()
}
else{
NSLog("CORRECT!")
}
}
@IBAction func Btn2(sender: UIButton) {
if AnswerNumber == 1 {
pickQuestion()
}
else{
NSLog("Wrong!")
}
}
@IBAction func Btn3(sender: UIButton) {
if AnswerNumber == 2 {
pickQuestion()
}
else{
NSLog("Wrong!")
}
}
@IBAction func Btn4(sender: UIButton) {
if AnswerNumber == 3 {
pickQuestion()
}
else{
NSLog("Wrong!")
}
}
@IBAction func reset(sender: UIButton) {
pickQuestion() //- here is the problem, what to put here? it only continue with quiz it does not restart it, tried with reset numbers and array, but don't work
}
}
答案 0 :(得分:1)
创建一个功能来重新生成您的问题:
func freshQuestions() -> [Questions] {
return [Question(Question: "Jesi pojeo?" , Answers: ["Jesam", "Nisam", "Budem", "Fuj"], Answer: 0),
Question(Question: "Najvisi vrh piramide?" , Answers: ["10", "39", "40", "55"], Answer: 2),
Question(Question: "Jesi popusio?" , Answers: ["Jesam", "Nisam", "Budem", "Fuj"], Answer: 0),
Question(Question: "Jesi bio vani?" , Answers: ["Jesam", "Nisam", "Budem", "Fuj"], Answer: 3)]
}
更改viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
Questions = freshQuestions()
pickQuestion()
}
然后:
@IBAction func reset(sender: UIButton) {
Questions = freshQuestions()
QNumber = 0
pickQuestion() //- here is the problem, what to put here? it only continue with quiz it does not restart it, tried with reset numbers and array, but don't work
}
如果你在自己的resetQuiz()
函数中将这两个新行包装在reset-function中,那就更好了。
答案 1 :(得分:1)
您的重置按钮需要重新填充您的问题数组,因为每次调用pickQuestion()
时,您都会从数组中删除问题。
您可以通过将此数组添加到重置功能来重新声明阵列。
Questions = [Question(Question: "Jesi pojeo?" , Answers: ["Jesam", "Nisam", "Budem", "Fuj"], Answer: 0),
Question(Question: "Najvisi vrh piramide?" , Answers: ["10", "39", "40", "55"], Answer: 2),
Question(Question: "Jesi popusio?" , Answers: ["Jesam", "Nisam", "Budem", "Fuj"], Answer: 0),
Question(Question: "Jesi bio vani?" , Answers: ["Jesam", "Nisam", "Budem", "Fuj"], Answer: 3)]
理想情况下,为了减少重复的代码,你应该将问题数组的总体重构为它自己的函数。