UISlider, save status

倖福魔咒の 提交于 2019-12-13 07:45:09

问题


My question is about the UISlider. I managed to implement everything but i don't know how i can save its status.

Ive looked everywhere but al the posts are in older versions of swift/xcode. So the question is how do i save its status so that when i go to another view and then come back the status is still the same.

Thanks very much!

import UIKit

class SettingsViewController: UIViewController {

  var sequeInt = 0

  let savedWordLength = NSUserDefaults.standardUserDefaults()

  override func viewDidLoad() {
    super.viewDidLoad()
  }

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

  @IBOutlet weak var wordLength: UISlider!

  @IBOutlet weak var wordLengthValue: UILabel!

  var selectedValue: Int = 5

  @IBAction func valueChanged(sender: UISlider) {

    selectedValue = Int(sender.value)

    savedWordLength.setInteger(selectedValue, forKey: "myInt")

    let ourInt = savedWordLength.integerForKey("myInt")
    sequeInt = ourInt
    print (sequeInt)

    wordLengthValue.text = String(ourInt)
}

回答1:


UISlider value property is a Float so you can use NSUserDefault's method setFloat to save its value and retrieve it next time your view appears using NSUserDefaults method floatForKey.

to save it:

NSUserDefaults.standardUserDefaults().setFloat(sender.value, forKey: "wordLength")

load it:

override func viewWillAppear(animated: Bool) {
    wordLength.setValue(NSUserDefaults.standardUserDefaults().floatForKey("wordLength"), animated: false)
}



回答2:


Follow these steps.

  1. In your view will appear.

    fontSlider.setValue(UserDefaults.standard.float(forKey: "slider_value"), animated: false)
    
  2. Take another outlet from storyBoard as "editingDidEnd". In that function:

    UserDefaults.standard.set(fontSlider.value, forKey: "slider_value")
    
  3. And finally in your ValueChanged Outlet.

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


来源:https://stackoverflow.com/questions/34108248/uislider-save-status

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!