gpt4 book ai didi

ios - 尝试让测验功能快速运行

转载 作者:行者123 更新时间:2023-11-30 12:32:39 25 4
gpt4 key购买 nike

我正在尝试让测验功能正常工作,而我的问题是我正在尝试在第二个 View Controller 上打印分数。当我运行模拟器时,它将用户带到第二个 View Controller ,但不打印分数。

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

第二个 View Controller 是

    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.
}
*/

}

最佳答案

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

此外,您正在更改此局部变量的值,而没有在任何地方更改全局变量的值。这种混淆很可能是由于变量的命名相似造成的。

只是一个建议,避免使用全局变量来传递数据。研究在 VC 之间传递数据的委托(delegate)协议(protocol)方式。

关于ios - 尝试让测验功能快速运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43306911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com