试图让测验函数在swift中工作

时间:2017-04-09 12:55:51

标签: ios swift

我正在尝试使用测验功能,我的问题是我正在尝试在第二个视图控制器上打印乐谱。当我运行模拟器时,它将用户带到第二个视图控制器,但分数不会打印。

import UIKit

var CurrentQuestion = ""   // global variable to be accessed

class ViewController: UIViewController {

    // create an array to store queations 
    let questions = ["favourite pet?", "favourite colour", "where was i born"]

// multiple arrays that contain strings, right anwser is first
let anwsers = [["dog", "cat", "bird"], ["blue", "black", "green"], ["tokyo", "tennese", "england"]]

// variables 
// keeps track of what question were at
var currentQuestion = 0
var rightAnwserPlacement:UInt32 = 0  // where right anwser is located
var points = 0;     // counts number of points - score



//lbl
@IBOutlet weak var lbl: UILabel!

//button
@IBAction func btn(_ sender: UIButton) {
    if (sender.tag == Int(rightAnwserPlacement))
    {
        print ("right")
        points += 1
    }
    else
    {
        print ("wrong")
    }

    if (currentQuestion != questions.count)
    {
        newQuestion()
    }
    else
    {
        performSegue(withIdentifier: "showScore", sender: self)
    }

}


override func viewDidAppear(_ animated: Bool) {

    newQuestion()
}






// function that displays a new question                      sets up interface for the new questions
func newQuestion()
{
    lbl.text = questions[currentQuestion]

    rightAnwserPlacement = arc4random_uniform(3)+1  // random button

    // create a button
    var button:UIButton = UIButton()
    var x = 1
    for i in 1...3
    {
        // create a button 
        button = view.viewWithTag(i) as! UIButton

        if (i == Int(rightAnwserPlacement))         // checks to see if it matches the right anwser
        {
            button.setTitle(anwsers[currentQuestion][0], for: .normal)
        }
        else {
            button.setTitle(anwsers[currentQuestion][x], for: .normal)
            x = 2

        }

    }

     currentQuestion += 1
}






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

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

第二个视图控制器是

    import UIKit
class second: UIViewController {

    @IBOutlet weak var label: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()


}
override func viewDidAppear(_ animated: Bool) {
    label.text = CurrentQuestion
}

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


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.


     // Pass the selected object to the new view controller.
    }
    */

}

1 个答案:

答案 0 :(得分:0)

您已将CurrentQuestion设置为全局字符串变量,稍后尝试将其值增加1?那是什么意思?此外,您在类中有一个与currentQuestion同名的局部变量。当然这两个是不同的变量,但你应该认真考虑重命名你的变量。

此外,您正在更改此局部变量的值,而无需更改全局变量的值。很可能这种混淆是由于变量的类似命名。

只是一个建议,避免使用全局变量传递数据。查看在VC之间传递数据的委托协议方式。