gpt4 book ai didi

ios - UserDefaults 不显示数据?

转载 作者:行者123 更新时间:2023-11-30 13:42:59 25 4
gpt4 key购买 nike

我正在呈现一个带有一些文本字段和按钮的 View ,用户可以在该文本字段中输入一些数据,当用户单击保存按钮时,该数据将保存在 NSUserdefaults 中并显示在另一个 View 上,但它不起作用..任何建议请..

View2 code Which is presented over view1

//The default key value
var _billingAddress = "billingAddress"

//The address text field in present view
@IBOutlet weak var address: UITextField!


//The button action code
@IBAction func saveBtn(sender: AnyObject) {

let defaults = NSUserDefaults.standardUserDefaults()
defaults.setValue(address.text, forKey:_billingAddress)

}

View1 code to access the key value data

//Label where i want to show the saved value
@IBOutlet weak var billingAddresh: UILabel!

let defaults = NSUserDefaults.standardUserDefaults()
if let name = defaults.stringForKey(_billingAddress)
//Here it shows the error use of unresolved identifier _billingAddress
{
billingAddresh.text = name
}

它没有显示任何值(value)谢谢您的帮助..

最佳答案

当您添加时间时,我们需要defaults.synchronize()

let defaults = NSUserDefaults.standardUserDefaults()
defaults.setValue(address.text, forKey:"billingAddress")

// if you want use else ignore
defaults.synchronize()

检索的是

let defaults = NSUserDefaults.standardUserDefaults()
if let name = defaults.stringForKey("billingAddress") // not "_billingAddress"
{
billingAddresh.text = name
}

// no need in here
defaults.synchronize()

关于ios - UserDefaults 不显示数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35339566/

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