arrays - “下一步”按钮在我的 Swift 测验应用程序中不起作用

标签 arrays swift

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

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
)

]

最佳答案

问题可能出在 QlistsA 上。它是什么?类还是全局数组?它必须被初始化,我不认为你在这段代码的任何地方都这样做了。 因此,您将 qlists 设置为 nil 值,然后尝试访问它,应用程序崩溃了。 你可以调试它并在 qlists = QlistsA 之后设置一个断点,看看它确实是 nil。

关于arrays - “下一步”按钮在我的 Swift 测验应用程序中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33061109/

相关文章:

java - 将一定范围的数字添加到 Java 数组

c# - 如何将 list<> 转换为多维数组?

ios - Swift 字典映射 - 在闭包中初始化

c# - 如何在没有副本的情况下将结构转换为字节数组?

arrays - 谷歌表格 : Array formula for fixed cell value

php array_splice() 空数组

ios - 如何在 Swift 中使用 Reachability Objective-C 类

swift - Swift 中的 Pointwise Equal、Pointwise less than 和 Pointwise greater 的功能是什么?

ios - 单击提交按钮后从 ortuman/Swift Forms 检索数据

swift - 在这种情况下,使用常量值作为 "nil"(没有值)是一个好的做法吗?