NSUser默认不能正确保存?

时间:2015-11-25 18:25:46

标签: ios swift nsuserdefaults

所以最近我一直在制作一个有文本字段和标签的练习应用程序。当您在文本字段中输入内容并点击保存时,它应将标签更改为该文本并保存。它保存的原因是当你打开应用程序时,标签的文本等于你保存的最后一件事。如果在文本框中点击保存新字符串,文本将再次保存。

我遇到的问题是标签不会出现文字,直到您保存新内容并且字符串不能保存,因此即使您保存了某些内容,它们也没有任何内容。

以下是我在ViewController.swift中的代码:

    //
//  ViewController.swift
//  Help
//
//  Created by Lucas on 9/30/15.
//  Copyright (c) 2015 Lucas. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet var Savedlbl: UILabel!
    @IBOutlet var Textfield: UITextField!
    @IBOutlet var Label: UILabel!


    var current = ""
    var Saved = ""

    override func viewDidLoad() {
        super.viewDidLoad()

        let currentDefault = NSUserDefaults.standardUserDefaults()

      Savedlbl.text = Saved



        if(currentDefault.valueForKey("Saved") != nil)
        {
            self.Saved = currentDefault.valueForKey("Saved") as! NSString! as String

        }
        // Do any additional setup after loading the view, typically from a nib.
    }
    @IBAction func Set(sender: AnyObject) {


        setall()


    }
func setall()
    {
        current = Textfield.text!
        Label.text = Textfield.text!



        let currentDefault = NSUserDefaults.standardUserDefaults()

       Saved = (currentDefault.valueForKey("saved") as? String)!
        currentDefault.setValue(Saved, forKey: "saved")


        Savedlbl.text = Textfield.text



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

这里是我认为问题在视图中确实有意义的负载。当应用加载时,不将其设置为最后保存的内容。

    import UIKit

class ViewController: UIViewController {

    @IBOutlet var Savedlbl: UILabel!
    @IBOutlet var Textfield: UITextField!
    @IBOutlet var Label: UILabel!


    var current = ""
    var Saved = ""

    override func viewDidLoad() {
        super.viewDidLoad()

        let currentDefault = NSUserDefaults.standardUserDefaults()

      Savedlbl.text = Saved



        if(currentDefault.valueForKey("Saved") != nil)
        {
            self.Saved = currentDefault.valueForKey("Saved") as! NSString! as String

        }
        // Do any additional setup after loading the view, typically from a nib.
    }

谢谢,如果您需要更多信息,请告诉我,我会尽快提供!

2 个答案:

答案 0 :(得分:1)

不要使用valueForKey / setValue:forKey:。那些是KVC方法。您需要setObject:forKey:objectForKey:

答案 1 :(得分:0)

你的问题是你正在以错误的顺序做事。在viewDidLoad中,您在从用户默认值中检索值之前设置文本字段,并在setall中重新保存以前保存的值,而不是新值。

尝试:

import UIKit

class ViewController: UIViewController {

    var saved=""
    var current=""
    @IBOutlet var Savedlbl: UILabel!
    @IBOutlet var Textfield: UITextField!
    @IBOutlet var Label: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        let currentDefault = NSUserDefaults.standardUserDefaults()
        self.saved=currentDefault.stringForKey("Saved") ?? ""
        self.Savedlbl.text=self.saved

    }

    @IBAction func Set(sender: AnyObject) {
        setall()
    }

    func setall() {
        self.current = self.Textfield.text!
        self.Label.text = self.current

        let currentDefault = NSUserDefaults.standardUserDefaults()
        currentDefault.setObject(self.current,forKey:"Saved")

        self.Savedlbl.text = self.Textfield.text
    }

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

此外,按照惯例,变量应以小写字母开头,类以大写字母开头。我将Saved更改为saved并将Current更改为current但我没有更改IBOutlets,因为您需要在IB中重新建立连接,但您应该这样做。