将UITextFiled的内容保存到NSUserDefaults

时间:2022-08-24 15:53:48

I'm trying to save a textfield and then retrieve it back in the view did load area here is my code:

我正在尝试保存文本字段,然后在视图中将其检索回来,这里的加载区域是我的代码:

@IBAction func player1button(sender: AnyObject)
{
    NSUserDefaults.standardUserDefaults().setValue(textfield1.text!, forKey:"firstPlayer")
}

override func viewDidLoad() {
    super.viewDidLoad()

    textfield1.text = (NSUserDefaults.standardUserDefaults().valueForKey("firstPlayer") as! String)
}

I'm getting this error:

我收到这个错误:

terminating with uncaught exception of type NSException

以NSException类型的未捕获异常终止

2 个解决方案

#1


0  

Use stringForKey when retrieving a String from NSUserDefaults:

从NSUserDefaults检索字符串时使用stringForKey:

NSUserDefaults.standardUserDefaults().stringForKey("firstPlayer")

#2


0  

First add a target to your textField for EditingDidEnd and a method to save the textField.text property to NSUserDefault. Then you just need to load it next time your view loads (BTW you should use NSUserDefaults method stringForKey. You just need to use "??" the nil coalescing operator to provide default value in case of nil.

首先将一个目标添加到您的textField以进行EditingDidEnd,并将一个方法保存到NSUserDefault,以保存textField.text属性。然后你只需要在下次加载视图时加载它(顺便说一下你应该使用NSUserDefaults方法stringForKey。你只需要使用“??”nil coalescing运算符来提供默认值,如果是nil。

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var textField: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        textField.text = NSUserDefaults().stringForKey("textField") ?? ""
        textField.addTarget(self, action: "editingDidEnd:", forControlEvents: UIControlEvents.EditingDidEnd)
    }
    func editingDidEnd(sender:UITextField){
        NSUserDefaults().setObject(sender.text!, forKey: "textField")
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

#1


0  

Use stringForKey when retrieving a String from NSUserDefaults:

从NSUserDefaults检索字符串时使用stringForKey:

NSUserDefaults.standardUserDefaults().stringForKey("firstPlayer")

#2


0  

First add a target to your textField for EditingDidEnd and a method to save the textField.text property to NSUserDefault. Then you just need to load it next time your view loads (BTW you should use NSUserDefaults method stringForKey. You just need to use "??" the nil coalescing operator to provide default value in case of nil.

首先将一个目标添加到您的textField以进行EditingDidEnd,并将一个方法保存到NSUserDefault,以保存textField.text属性。然后你只需要在下次加载视图时加载它(顺便说一下你应该使用NSUserDefaults方法stringForKey。你只需要使用“??”nil coalescing运算符来提供默认值,如果是nil。

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var textField: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        textField.text = NSUserDefaults().stringForKey("textField") ?? ""
        textField.addTarget(self, action: "editingDidEnd:", forControlEvents: UIControlEvents.EditingDidEnd)
    }
    func editingDidEnd(sender:UITextField){
        NSUserDefaults().setObject(sender.text!, forKey: "textField")
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}