下一个按钮在我的Swift测验应用

时间:2015-10-11 02:59:41

标签: arrays swift

Screenshot这是一个很受欢迎的练习测验应用,我也尝试过。我将数组QlistsA放在一个单独的文件中。与我沟通对我来说仍然是一个挑战。一切正常,但我现在仍然坚持按下一个按钮转到下一个问题。下一个按钮功能不起作用,但粉碎。知道我在哪里犯了错误吗?真的很感激! (在截图中,我还没有制作标签"得分"然而。无视。我希望我能在以后制作。)

import UIKit

struct Question {
var question: String!
var answerList: [String] = []
var answerNum: Int!
} 

class ViewController: UIViewController {

@IBOutlet weak var qNum: UILabel!
@IBOutlet weak var question: UILabel!
@IBOutlet var aButtons: [UIButton]!
@IBOutlet weak var smileLabel1: UILabel!
@IBOutlet weak var smileLabel2: UILabel!
@IBOutlet weak var nextButton: UIButton!


var qlists = [Question]()
var qNumber = Int()
var answerNum = Int()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    qlists = qlistsA

    PickQuiz()
    Hide()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// Just set a initial view.
func PickQuiz() {

        question.text = qlists[0].question
        answerNum = qlists[0].answerNum

            for i in 0..<aButtons.count{
            aButtons[i].setTitle(qlists[0].answerList[i], forState: UIControlState.Normal)
            }
}

    func Hide(){
    smileLabel1.hidden = true
    smileLabel2.hidden = true
}


@IBAction func answer1(sender: AnyObject) {
    if answerNum == 0 {
        smileLabel1.hidden = false
        smileLabel2.hidden = true
        smileLabel1.text = ":)"

    } else {
        smileLabel2.hidden = true
        smileLabel1.hidden = false
        smileLabel1.text = ":("

    }
}

@IBAction func answer2(sender: AnyObject) {
     if answerNum == 1 {
        smileLabel1.hidden = true
        smileLabel2.hidden = false
        smileLabel2.text = ":)"

    }
    else {
        smileLabel1.hidden = true
        smileLabel2.hidden = false
        smileLabel2.text = ":("
    }

    }

@IBAction func gotoNext(sender: AnyObject) {

    Hide()

    for qNumber = 1; qNumber < qlists.count; qNumber++ {
        question.text = qlists[qNumber].question
        answerNum = qlists[qNumber].answerNum
        print("The loop were excuted \(qNumber) times") // The loop is working, but the labels display only the last entries.

        for i in 0..<aButtons.count{
            aButtons[i].setTitle(qlists[qNumber].answerList[i], forState: UIControlState.Normal)
        }

    }

}

}

数组如下:

import Foundation

var qlistsA = [
Question(
    question: "1. What is 10 times 2?",
    answerList: ["20", "12"],
    answerNum: 0
),

Question(
    question: "2. What is 13 mod 5?",
    answerList: ["5", "3"],
    answerNum: 1
),

Question(
    question: "3. Do you like UMBC?",
    answerList: ["Yes", "No"],
    answerNum: 0
)

1 个答案:

答案 0 :(得分:0)

问题可能是QlistsA。它是什么?类或全局数组?它必须被初始化,我不会在此代码中的任何位置执行此操作。 因此,您将qlists设置为零值,然后您尝试访问它并且应用程序崩溃。 您可以调试它并在qlists = QlistsA之后设置一个断点,并且看到它确实是零。