ios - iOS 应用程序的分数跟踪系统中的变量未更新

标签 ios swift

我编写了一个变量来跟踪迷你游戏中的分数,但在执行某些代码后它没有反射(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/

相关文章:

c++ - iOS 构建中的非法操作码

ios - 如何检测之前是否显示过位置服务弹出窗口

ios - EXC_BAD_ACCESS KERN_INVALID ADDRESS 在 iOS9 设备上崩溃

ios - TableView 自定义单元格设计问题

ios - Epos打印机iOS SDK

ios - 如何在基本 View 中间添加自定义 View

ios - 无法使用 XCode 8 将 UIView 上方的 UIScrollView 插入到另一个场景

ios - 使用时间和动画更改字符串数组中的 UILabel/UIButton 文本

ios - 从 UIPageViewController 点指示器中删除黑条

具有返回类型重载的 Swift 泛型函数调用函数