简体   繁体   English

如何将UISlider的当前值快速保存到我的NSUserDefaults中?

[英]How can I save the current value of an UISlider to my NSUserDefaults in swift?

I have an app with two settings components, a uiSlider and a uiSwitch . 我有一个带有两个设置组件的应用程序,一个uiSlider和一个uiSwitch

I know how to save option for the switch, this is what I'm doing: 我知道如何为交换机保存选项,这就是我正在做的:

@IBOutlet weak var settingA: UISwitch!

override func viewDidLoad() {

        settingA.on = NSUserDefaults.standardUserDefaults().boolForKey("settingA")
...
}

@IBAction func saveSettingA(sender: AnyObject) {
    print("setting A changed")
    NSUserDefaults.standardUserDefaults().setObject(settingA.on, forKey: "settingA")
    NSUserDefaults.standardUserDefaults().synchronize()
}

Now I want to do the same for the slider. 现在,我想对滑块做同样的事情。 I already created an outlet to the slider and to the label next to it (that shows the value of the slider): 我已经为滑块及其旁边的标签创建了一个出口(显示滑块的值):

@IBOutlet weak var sliderLabel: UILabel!
@IBOutlet weak var sliderSlider: UISlider!

and also I have a method for updating the label every time user moves the slider around: 还有一种方法,每次用户移动滑块时都会更新标签:

@IBAction func sliderValueChanged(sender: UISlider) {
    var currentValue = Int(sender.value)
    sliderLabel.text = "\(currentValue)"
}

But how can I save it to the NSUserDefaults every time user chooses new value? 但是,每次用户选择新值时,如何将其保存到NSUserDefaults

I'd suggest you connect/add your/another action via Storyboard in this way: 我建议您以这种方式通过Storyboard连接/添加/其他操作:

在此处输入图片说明

Special stress here is that you should really only store the value once the user releases the button (otherwise you would redundantly make store requests to NSUserDefaults , which I'm sure you don't want), hence you can see I selected Touch Up Outside instead of Value Changed for the event. 这里要特别强调的是,您仅应在用户释放按钮后才真正存储该值(否则,您将多余地向NSUserDefaults发出存储请求,我确定您不希望这样做),因此可以看到我选择了“ Touch Up Outside而不是为事件Value Changed The reason I phrased it like this ("connect/add your/another") is because you can connect multiple @IBAction s, for example one for the event Value Changed for when you want to observe the value changing and doing something when that happens and also as I demonstrated to act upon the user letting go of the slider. 我之所以这样说(“连接/添加您的/另一个”)是因为您可以连接多个@IBAction ,例如,一个Value Changed事件,当您要观察值更改时进行更改,并在发生这种情况时进行一些操作就像我演示过的,要让用户放开滑块。

Then, in order to store the value, you can do: 然后,为了存储值,您可以执行以下操作:

@IBAction func valueChangeEnded(slider: UISlider) {
    print("save to user defaults")
    NSUserDefaults.standardUserDefaults().setFloat(slider.value, forKey: "slider_value")
  }

And read it back in like this: 然后像这样读回:

let sliderValue = NSUserDefaults.standardUserDefaults().floatForKey("slider_value")

You should be able to use the same logic in saveSettingA inside of the sliderValueChanged function. 您应该能够在sliderValueChanged函数内部的saveSettingA中使用相同的逻辑。 Just save the sender.value object rather than the int value. 只需保存sender.value对象而不是int值即可。

swift 4.2 迅捷4.2

1.In your view will appear. 1.在您的视图中将出现。

fontSlider.setValue(UserDefaults.standard.float(forKey: "slider_value"), animated: false)

2.Take another outlet from storyBoard as "editingDidEnd". 2.从storyBoard处获取另一个编辑点作为“ editingDidEnd”。 In that function: 在该功能中:

UserDefaults.standard.set(fontSlider.value, forKey: "slider_value")

3.And finally in your ValueChanged Outlet. 3.最后在您的ValueChanged插座中。

UserDefaults.standard.set(fontSlider.value, forKey: "slider_value")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM