使用NSUserDefaults保存标签

时间:2015-11-24 16:41:33

标签: ios swift nsuserdefaults

我已经将我的高分设为标签。现在我试图保存这个标签以备将来使用。如何保存标签的文字?我认为你必须使用NSUserDefaults。任何回复都将非常感谢。 这是我的代码:

import UIKit

class ViewController3: UIViewController , UITextFieldDelegate {

    @IBOutlet weak var LABEL20: UILabel!
    @IBOutlet weak var textFieldName: UITextField!
    @IBOutlet weak var label1: UILabel!
    @IBOutlet weak var label2: UILabel!
    @IBOutlet weak var label3: UILabel!
    @IBOutlet weak var label4: UILabel!
    @IBOutlet weak var label5: UILabel!
    @IBOutlet weak var label6: UILabel!
    @IBOutlet weak var label7: UILabel!
    @IBOutlet weak var label8: UILabel!
    @IBOutlet weak var label9: UILabel!
    @IBOutlet weak var label10: UILabel!
    @IBOutlet weak var score1: UILabel!
    @IBOutlet weak var score2: UILabel!
    @IBOutlet weak var score3: UILabel!
    @IBOutlet weak var score4: UILabel!
    @IBOutlet weak var score5: UILabel!
    @IBOutlet weak var score6: UILabel!
    @IBOutlet weak var score7: UILabel!
    @IBOutlet weak var score8: UILabel!
    @IBOutlet weak var score9: UILabel!
    @IBOutlet weak var score10: UILabel!


    let score11 = Int()
    let score12 = Int()
    let score13 = Int()
    let score14 = Int()
    let score15 = Int()
    let score16 = Int()
    let score17 = Int()
    let score18 = Int()
    let score19 = Int()
    let score20 = Int()

    var highScore = Int()




    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func textFieldShouldReturn(textField: UITextField) -> Bool{
        textFieldName.resignFirstResponder()
        let name = textFieldName.text
        textFieldName.text = ""

        if highScore >= score11 {
            LABEL20.alpha = 0
            score1.text = String(highScore)
            score11 == highScore
            label1.text = name
            label1.alpha = 1
            score1.alpha = 1
            NSUserDefaults.standardUserDefaults() == score12
        }
        else if highScore >= score12{
            LABEL20.alpha = 0
            score2.text = String(highScore)
            score12 == highScore
            label2.text = name
            label2.alpha = 1
            score2.alpha = 1
        }
        else if highScore >= score13{
            LABEL20.alpha = 0
            score3.text = String(highScore)
            score13 == highScore
            label3.text = name
            label3.alpha = 1
            score3.alpha = 1
        }
        else if highScore >= score14{
            LABEL20.alpha = 0
            score4.text = String(highScore)
            score14 == highScore
            label4.text = name
            label4.alpha = 1
            score4.alpha = 1
        }
        else if highScore >= score15{
            LABEL20.alpha = 0
            score5.text = String(highScore)
            score15 == highScore
            label5.text = name
            label5.alpha = 1
            score5.alpha = 1
        }
        else if highScore >= score16{
            LABEL20.alpha = 0
            score6.text = String(highScore)
            score16 == highScore
            label6.text = name
            label6.alpha = 1
            score6.alpha = 1
        }
        else if highScore >= score17{
            LABEL20.alpha = 0
            score7.text = String(highScore)
            score17 == highScore
            label7.text = name
            label7.alpha = 1
            score7.alpha = 1
        }
        else if highScore >= score18{
            LABEL20.alpha = 0
            score8.text = String(highScore)
            score18 == highScore
            label8.text = name
            label8.alpha = 1
            score8.alpha = 1
        }
        else if highScore >= score19{
            LABEL20.alpha = 0
            score9.text = String(highScore)
            score19 == highScore
            label9.text = name
            label9.alpha = 1
            score9.alpha = 1
        }
        else if highScore >= score20{
            LABEL20.alpha = 0
            score10.text = String(highScore)
            score20 == highScore
            label10.text = name
            label10.alpha = 1
            score10.alpha = 1
        }

        else{

            LABEL20.alpha = 1

        }


        return true
    }
}

1 个答案:

答案 0 :(得分:0)

这是您使用NSUserDefaults保存分数的方法

let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject("your score", forKey: "score")

这就是你将如何检索你的分数

if let score = defaults.stringForKey("score")
{
        println(score)
}