首页 > 解决方案 > 如何在Xcode swift中为ui标签使用if语句

问题描述

我的代码:

    import UIKit

class ViewController: UIViewController {
    

    var score = 0
    
        
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    
    @IBAction func textfield(_ sender: Any) {
         
        
    }
    @IBAction func Button(_ sender: Any) {


    
  let imageName = "FB copy-3.png"
        let image = UIImage(named: imageName)
        let imageView = UIImageView(image: image!)
        imageView.frame = CGRect(x: 64.5, y: 413.5, width: 100, height: 100); view.addSubview(imageView)

        Counterlabel.text = String(score)
        
        if score >= 500 && score <= 998 {
            
            score += 2
            imageView.image = UIImage(named:"500button")
        }
        else if score >= 998 && score <= 1995 {
            
            score += 5
        }
        else if score >= 1995 && score <= 4000 {
            
            score += 10
            imageView.image = UIImage(named:"2000button")
           
        }
        else if score >= 4000 && score <= 10000 {
            
            score += 20
            
        }
        else if score >= 10000 && score <= 25000 {
            score += 50
        }
        else if score >= 25000 && score <= 75000 {
            
            score += 100
            
        }
        else if score >= 75000{
            
            score += 250
            
        }
        else {
            
            score += 1
            
        }
    }
    
    @IBAction func buttonimage(_ sender: Any) {
        
    }
    
    
    
    @IBOutlet weak var Counterlabel: UILabel!
}

所以我最近开始编码并且不太了解我想创建一个小游戏,这是我的问题:

我首先想在我的游戏中添加一个作弊码,所以我有一个文本标签,我想要它,这样如果我的文本标签文本是 1811(或任何有效),那么得分 += 50000

这就是我一直面临的问题

还有一件更重要的事情(如果可能的话)我真的很想知道如何保存 gtame 数据,所以如果再次播放,进度会被保存。我要保存的唯一进度是数字。

排行榜也不错

以上任何一项都会对我有帮助,我真的很感激。

这就是我的故事板的样子:

主故事板

标签: iosswiftxcodeswiftui

解决方案


您可以使用类似检查 textField 值

if textField.text == "11811" { socre += 5000 }

推荐阅读