[英]Save label.text in swift4
Im currently developing a iOS app in which im going to keep track of our warehouse stock. 我目前正在开发一个iOS应用,该应用将跟踪我们的仓库库存。 It's a pretty simple app an just contains a lable and an stepper.
这是一个非常简单的应用程序,只包含一个标签和一个步进器。 The app is pretty much finished, but i don't get how to save the changed value of the label.
该应用程序几乎完成了,但是我不知道如何保存标签的更改值。 I want to save it automatically so that when someone presses the "+" on the stepper, the value should save without pressing a extra save button
我想自动保存它,以便当有人在步进器上按“ +”时,该值应保存而无需按下额外的保存按钮
Current code: 当前代码:
//montageplatte
@IBOutlet weak var lbl_montageplatte: UILabel!
@IBAction func stepper_montageplatte(_ sender: UIStepper)
{
lbl_montageplatte.text = Int(sender.value).description
}
you can save it in UserDefaults
. 您可以将其保存在
UserDefaults
。
@IBAction func stepper_montageplatte(_ sender: UIStepper) {
lbl_montageplatte.text = Int(sender.value).description
UserDefaults.standard.set(String(sender.value), forKey: "lblMontageplatte")
}
To get back value you can do as follow... 要恢复价值,您可以按照以下步骤做...
if let lblValue = UserDefaults.standard.object(forKey: "lblMontageplatte") as? String {
print(lblValue)
lbl_montageplatte.text = lblValue
}
Simple solution: 简单的解决方案:
In the custom cell create an outlet for the stepper and a NSKeyValueObservation
property 在自定义单元格中,为步进器创建一个插座,并创建一个
NSKeyValueObservation
属性
@IBOutlet weak var stepper : UIStepper!
var stepperObservation : NSKeyValueObservation?
In cellForRow
in the controller add the key value observer 在控制器的
cellForRow
中添加键值观察器
cell.stepperObservation = cell.stepper.observe(\.value, options: [.new]) { (stepper, change) in
print(change.newValue!)
}
Rather than printing the new value update the model (the item for the particular index path) and save the datasource array if necessary. 无需打印新值,而是更新模型(特定索引路径的项目)并在必要时保存数据源数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.