即使重启iPhone,如何保存得分?

时间:2015-07-09 17:37:19

标签: ios swift

我制作了一个应用程序Single Application。我放了一个名为Dim LC1 As Long, ColNum1 As Long Application.ScreenUpdating = False For ColNum1 = 8 To Cells(1, Columns.Count).End(xlToLeft).Column For LC1 = 2 To Cells(Rows.Count, ColNum1).End(xlUp).Row If Val(CStr(Cells(LC1, ColNum1))) < 0 Then Cells(LC1, ColNum1) = 0 Next LC1 Next ColNum1 Application.ScreenUpdating = True 的按钮,一个名为CountUpButton的标签和另一个按钮CountLabelResetButton的功能是按下按钮时,CountUpButton必须更改CountLabel,按下+ 1时,ResetButton必须转为{{1} }}。但问题是每当我重启我的iPhone或App时,CountLabel都会变回0.它应该保存0,因此只有在按下CountLabel时才需要更改。 / p>

此功能的代码是:

CountLabel

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

ResetButton

1 个答案:

答案 0 :(得分:1)

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)"
    }
}