gpt4 book ai didi

ios - 即使我重新启动 iPhone,如何保存分数?

转载 作者:行者123 更新时间:2023-11-29 01:51:45 25 4
gpt4 key购买 nike

我制作了一个应用程序单一应用程序。我放置了一个名为 CountUpButton 的按钮、一个名为 CountLabel 的标签和另一个按钮 ResetButtonCountUpButton的功能是当按下按钮时,CountLabel必须改变+1,当按下ResetButton时, CountLabel 必须变为 0。但问题是,每当我重新启动 iPhone 或应用程序时,CountLabel 就会变回 0。它应该保存 CountLabel,因此只有在 时才需要更改ResetButton 被按下。

该函数的代码为:

var CountNumber = 0

@IBAction func ResetButton(sender: UIButton) {
CountNumber = 0
CountLabel.text = "0"
}

@IBOutlet var CountLabel: UILabel!

@IBAction func CountUpButton(sender: UIButton) {
CountNumber += 1
CountLabel.text = "\(CountNumber)"
}

我用于保存“CountLabel”的代码是:

func saveCounted(){
let defeaults = NSUserDefaults.standardUserDefaults()
defeaults.setInteger(CountNumber, forKey: "CountLabel")
}

最佳答案

import UIKit

class ViewController: UIViewController {
@IBOutlet weak var counterLabel: UILabel!
var counter: Int {
return NSUserDefaults().integerForKey("counter")
}
override func viewDidLoad() {
super.viewDidLoad()
counterLabel.text = "\(counter)"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func countUp(sender: AnyObject) {
NSUserDefaults().setInteger(counter+1, forKey: "counter")
counterLabel.text = "\(counter)"

}
@IBAction func resetCounter(sender: AnyObject) {
NSUserDefaults().removeObjectForKey("counter")
counterLabel.text = "\(counter)"
}
}

关于ios - 即使我重新启动 iPhone,如何保存分数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31324533/

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