我已经从youtube教程中创建了一个测验应用。现在我想添加一个分数,以便玩家查看它。如果您选择选项a,该应用程序可以添加3个点,如果选择b则添加2个,如果选择c,则添加1个。
但每当我更改问题时,分数值会回到0,我该如何解决?
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var Question: UILabel!
@IBOutlet weak var answer1: UIButton!
@IBOutlet weak var answer2: UIButton!
@IBOutlet weak var answer3: UIButton!
@IBOutlet weak var nextquestion: UIButton!
@IBOutlet weak var results: UILabel!
var questionnumber = Int()
var total = Int()
override func viewDidLoad() {
super.viewDidLoad()
questionnumber = Int(arc4random_uniform(15))
total = 0
switch questionnumber {
case 0:
Question.text = "Aproximadamente, ¿cuántas veces al día usas el coche?"
answer1.setTitle("3 veces", for: UIControlState.normal)
answer2.setTitle("5 veces", for: UIControlState.normal)
answer3.setTitle("+ 6 veces", for: UIControlState.normal)
break
case 1:
Question.text = "¿Cuánto te tardas bañándote?"
answer1.setTitle("5-10 minutos", for: UIControlState.normal)
answer2.setTitle("10-15 minutos", for: UIControlState.normal)
answer3.setTitle("+ 15 minutos", for: UIControlState.normal)
break
case 2:
Question.text = "¿Separas la basura?"
answer1.setTitle("Sí", for: UIControlState.normal)
answer2.setTitle("De vez en cuando", for: UIControlState.normal)
answer3.setTitle("No", for: UIControlState.normal)
break
case 3:
Question.text = "¿Dejas correr el agua cuando te lavas los dientes?"
answer1.setTitle("Sí", for: UIControlState.normal)
answer2.setTitle("Algunas veces", for: UIControlState.normal)
answer3.setTitle("No", for: UIControlState.normal)
break
case 4:
Question.text = "¿Lavas tu coche con cubeta o manguera?"
answer1.setTitle("Cubeta", for: UIControlState.normal)
answer2.setTitle("Manguera", for: UIControlState.normal)
answer3.setTitle("", for: UIControlState.normal)
break
case 5:
Question.text = "¿Reciclas o reutilizas el papel?"
answer1.setTitle("Sí", for: UIControlState.normal)
answer2.setTitle("Algunas veces", for: UIControlState.normal)
answer3.setTitle("No", for: UIControlState.normal)
break
case 6:
Question.text = "¿Usas las bolsas ecológicas en el supermercado?"
answer1.setTitle("Sí", for: UIControlState.normal)
answer2.setTitle("De vez en cuando", for: UIControlState.normal)
answer3.setTitle("No", for: UIControlState.normal)
break
case 7:
Question.text = "¿Pones las pilas en contenedores especiales?"
answer1.setTitle("Sí", for: UIControlState.normal)
answer2.setTitle("Algunas veces", for: UIControlState.normal)
answer3.setTitle("No", for: UIControlState.normal)
break
case 8:
Question.text = "¿Afinas tu coche cuando te toca?"
answer1.setTitle("Sí", for: UIControlState.normal)
answer2.setTitle("De vez en cuando", for: UIControlState.normal)
answer3.setTitle("No", for: UIControlState.normal)
break
case 9:
Question.text = "¿Reutilizas las botellas de agua?"
answer1.setTitle("Sí", for: UIControlState.normal)
answer2.setTitle("Algunas veces", for: UIControlState.normal)
answer3.setTitle("No", for: UIControlState.normal)
break
case 10:
Question.text = "¿Qué tan seguido usas la lavadora?"
answer1.setTitle("1-2 veces por semana", for: UIControlState.normal)
answer2.setTitle("3-4 veces por semana", for: UIControlState.normal)
answer3.setTitle("+ 5 veces por semana", for: UIControlState.normal)
break
case 11:
Question.text = "¿Utilizas popote cuando tomas líquidos?"
answer1.setTitle("Sí", for: UIControlState.normal)
answer2.setTitle("De vez en cuando", for: UIControlState.normal)
answer3.setTitle("No", for: UIControlState.normal)
break
case 12:
self.Question.text = "¿Tiras basura en la calle/mar?"
self.answer1.setTitle("Sí", for: UIControlState.normal)
answer2.setTitle("Algunas veces", for: UIControlState.normal)
answer3.setTitle("No", for: UIControlState.normal)
break
case 13:
Question.text = "¿Utilizas calentador solar en tu casa?"
answer1.setTitle("Sí", for: UIControlState.normal)
answer2.setTitle("No", for: UIControlState.normal)
answer3.setTitle("", for: UIControlState.normal)
break
case 14:
Question.text = "¿Dejas luces encendidas cuando no es necesario?"
answer1.setTitle("Sí", for: UIControlState.normal)
answer2.setTitle("Algunas veces", for: UIControlState.normal)
answer3.setTitle("No", for: UIControlState.normal)
break
default:
break
}
// 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.
}
// Add a target to your button
// Create the method you want to call (see target before)
@IBAction func answer1pressed(_ sender: Any) {
total = total + 3
results.text = "Total:\(total)"
}
@IBAction func answer2pressed(_ sender: Any) {
total = total + 2
results.text = "Total:\(total)"
}
@IBAction func answer3pressed(_ sender: Any) {
total = total + 1
results.text = "Total:\(total)"
}
@IBAction func nextquestion(_ sender: Any) {
viewDidLoad()
}
}
答案 0 :(得分:0)
您在viewDidLoad方法中设置了total的值,因为在按下nextQuestion按钮时调用此方法,总值将再次为0。您需要在viewDidLoad方法之外移动等于0。例如:
class ViewController: UIViewController
{
@IBOutlet weak var Question: UILabel!
@IBOutlet weak var answer1: UIButton!
@IBOutlet weak var answer2: UIButton!
@IBOutlet weak var answer3: UIButton!
@IBOutlet weak var nextquestion: UIButton!
@IBOutlet weak var results: UILabel!
var questionnumber = Int()
var total: Int = 0
override func viewDidLoad() {
super.viewDidLoad()
questionnumber = Int(arc4random_uniform(15))
switch questionnumber {
case 0:
Question.text = "Aproximadamente, ¿cuántas veces al día usas el coche?"
answer1.setTitle("3 veces", for: UIControlState.normal)
answer2.setTitle("5 veces", for: UIControlState.normal)
answer3.setTitle("+ 6 veces", for: UIControlState.normal)
break
case 1:
Question.text = "¿Cuánto te tardas bañándote?"
answer1.setTitle("5-10 minutos", for: UIControlState.normal)
answer2.setTitle("10-15 minutos", for: UIControlState.normal)
answer3.setTitle("+ 15 minutos", for: UIControlState.normal)
break
case 2:
Question.text = "¿Separas la basura?"
answer1.setTitle("Sí", for: UIControlState.normal)
answer2.setTitle("De vez en cuando", for: UIControlState.normal)
answer3.setTitle("No", for: UIControlState.normal)
break
case 3:
Question.text = "¿Dejas correr el agua cuando te lavas los dientes?"
answer1.setTitle("Sí", for: UIControlState.normal)
answer2.setTitle("Algunas veces", for: UIControlState.normal)
answer3.setTitle("No", for: UIControlState.normal)
break
case 4:
Question.text = "¿Lavas tu coche con cubeta o manguera?"
answer1.setTitle("Cubeta", for: UIControlState.normal)
answer2.setTitle("Manguera", for: UIControlState.normal)
answer3.setTitle("", for: UIControlState.normal)
break
case 5:
Question.text = "¿Reciclas o reutilizas el papel?"
answer1.setTitle("Sí", for: UIControlState.normal)
answer2.setTitle("Algunas veces", for: UIControlState.normal)
answer3.setTitle("No", for: UIControlState.normal)
break
case 6:
Question.text = "¿Usas las bolsas ecológicas en el supermercado?"
answer1.setTitle("Sí", for: UIControlState.normal)
answer2.setTitle("De vez en cuando", for: UIControlState.normal)
answer3.setTitle("No", for: UIControlState.normal)
break
case 7:
Question.text = "¿Pones las pilas en contenedores especiales?"
answer1.setTitle("Sí", for: UIControlState.normal)
answer2.setTitle("Algunas veces", for: UIControlState.normal)
answer3.setTitle("No", for: UIControlState.normal)
break
case 8:
Question.text = "¿Afinas tu coche cuando te toca?"
answer1.setTitle("Sí", for: UIControlState.normal)
answer2.setTitle("De vez en cuando", for: UIControlState.normal)
answer3.setTitle("No", for: UIControlState.normal)
break
case 9:
Question.text = "¿Reutilizas las botellas de agua?"
answer1.setTitle("Sí", for: UIControlState.normal)
answer2.setTitle("Algunas veces", for: UIControlState.normal)
answer3.setTitle("No", for: UIControlState.normal)
break
case 10:
Question.text = "¿Qué tan seguido usas la lavadora?"
answer1.setTitle("1-2 veces por semana", for: UIControlState.normal)
answer2.setTitle("3-4 veces por semana", for: UIControlState.normal)
answer3.setTitle("+ 5 veces por semana", for: UIControlState.normal)
break
case 11:
Question.text = "¿Utilizas popote cuando tomas líquidos?"
answer1.setTitle("Sí", for: UIControlState.normal)
answer2.setTitle("De vez en cuando", for: UIControlState.normal)
answer3.setTitle("No", for: UIControlState.normal)
break
case 12:
self.Question.text = "¿Tiras basura en la calle/mar?"
self.answer1.setTitle("Sí", for: UIControlState.normal)
answer2.setTitle("Algunas veces", for: UIControlState.normal)
answer3.setTitle("No", for: UIControlState.normal)
break
case 13:
Question.text = "¿Utilizas calentador solar en tu casa?"
answer1.setTitle("Sí", for: UIControlState.normal)
answer2.setTitle("No", for: UIControlState.normal)
answer3.setTitle("", for: UIControlState.normal)
break
case 14:
Question.text = "¿Dejas luces encendidas cuando no es necesario?"
answer1.setTitle("Sí", for: UIControlState.normal)
answer2.setTitle("Algunas veces", for: UIControlState.normal)
answer3.setTitle("No", for: UIControlState.normal)
break
default:
break
}
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func answer1pressed(_ sender: Any) {
total = total + 3
results.text = "Total:\(total)"
}
@IBAction func answer2pressed(_ sender: Any) {
total = total + 2
results.text = "Total:\(total)"
}
@IBAction func answer3pressed(_ sender: Any) {
total = total + 1
results.text = "Total:\(total)"
}
@IBAction func nextquestion(_ sender: Any)
{
viewDidLoad()
}
}
这部分是最重要的:
var total: Int = 0
override func viewDidLoad() {
super.viewDidLoad()
questionnumber = Int(arc4random_uniform(15))
switch questionnumber {
case 0: