2015-09-16 38 views
0

我跟着一个测验教程和一切工作,但是当我想添加重置按钮,所以测验可以重新开始它不起作用。这里的代码,我试图重置数字,我试图清空数组,但它不工作。请帮助。Swift Quiz - 重置按钮

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

回答

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()功能。

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)] 

理想情况下,为了减少重复代码,您应该将问题数组的总体重构为它自己的函数。