我编写了一个变量来跟踪迷你游戏中的分数,但在执行某些代码后它没有反射(reflect)更新的值。
我正在制作一个嵌入到 iOS 应用程序中的基本测验迷你游戏。它在独立的 View Controller 中运行,不与应用程序的任何其他部分交互。我的代码没有给出任何错误,但是当我尝试将 UILabel 设置为变量的值时,它没有反射(reflect)出任何变化...
@IBOutlet weak var imgItemImage: UIImageView!
@IBOutlet weak var lblItemName: UILabel!
@IBOutlet weak var lblScore: UILabel!
@IBAction func btnRedBin(_ sender: Any) {
trackScore(binChoice: "red")
}
@IBAction func btnYellowBin(_ sender: Any) {
trackScore(binChoice: "yellow")
}
@IBAction func btnGreenBin(_ sender: Any) {
trackScore(binChoice: "green")
}
var trashList:[String] = ["Can", "Brick", "Sandwich"]
var itemChoice:Int = 0
var score:Int = 0
var gameRound:Int = 0
func itemChange() {
if gameRound <= 3 {
itemChoice = Int.random(in: 0...((trashList.count)-1))
lblItemName.text = trashList[itemChoice]
imgItemImage.image = UIImage(named: (trashList[itemChoice]))
} else {
lblItemName.text = "You scored \(score) points!"
}
gameRound += 1
lblScore.text = "\(score)"
}
func trackScore(binChoice:String){
switch trashList{
case ["Can"]:
if binChoice == "red"{
score = score - 1
} else if binChoice == "yellow" {
score = score + 10
} else {
score = score - 5
}
case ["Brick"]:
if binChoice == "red"{
score = score + 10
} else if binChoice == "yellow" {
score = score - 1
} else {
score = score - 5
}
case ["Sandwich"]:
if binChoice == "red"{
score = score + 10
} else if binChoice == "yellow" {
score = score - 1
} else {
score = score - 5
}
default:
break
}
trashList.remove(at: itemChoice)
itemChange()
}
我希望变量“score”在按下其中一个按钮( Action )时发生变化。但是当我这样做并尝试通过将 UILabel“lblScore”设置为其值来检查时,我总是得到 0。
最佳答案
这就是您的 trackScore 函数的样子
func trackScore(binChoice:String){
switch binChoice:String{
case "Can":
if binChoice == "red"{
score = score - 1
} else if binChoice == "yellow" {
score = score + 10
} else {
score = score - 5
}
case "Brick":
if binChoice == "red"{
score = score + 10
} else if binChoice == "yellow" {
score = score - 1
} else {
score = score - 5
}
case "Sandwich":
if binChoice == "red"{
score = score + 10
} else if binChoice == "yellow" {
score = score - 1
} else {
score = score - 5
}
default:
break
}
trashList.remove(at: itemChoice)
itemChange()
}
关于ios - iOS 应用程序的分数跟踪系统中的变量未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56370531/