-2

I am trying to create a slider such that at the initial load of the app, the slider has a default value, say 5. But if I were to change the slider value to 10, and leave the VC and come back, I would like the value to be 10. I have tried implementing this into my app but every time I return to the VC after leaving, the value is reset to the value I gave it in the StoryBoard. My code does circumnavigate this by making a value of 0 into 1 (which will always be the case for the first time the code runs), but I don't believe it is elegant, and there are better alternatives. I think I am not using viewDidAppear and viewDidLoad correctly, but I am not quite sure how to improve my code. Below is my attempt:

var distanceMap: Int = Int()


import UIKit


class MapSettingsVC: UIViewController {

    //IBOutlet for the slider
    @IBOutlet weak var rangeSlider: UISlider!

    // label for slider value
    @IBOutlet weak var distanceLabel: UILabel!

    //changes to slider value
    @IBAction func distanceRange(_ sender: UISlider) {

        distanceMap = Int(sender.value)
        distanceLabel.text = String(distanceMap)
    }

    // iboutlet to go to another viewController
    @IBAction func backButtonTapped(_ sender: AnyObject) {
        self.performSegue(withIdentifier: "mapSettingsGoBack", sender: self)
    }






override func viewDidAppear(_ animated: Bool) {



    rangeSlider.value = Float(distanceMap)
    distanceLabel.text = String(Int(rangeSlider.value))

    // I have initialized the label to be 0, so this will be FIRST value when the VC is loaded, and the if statement always runs the first time 
    if distanceLabel.text == "0" {
        rangeSlider.value = 1
        distanceLabel.text = "1"

    }



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

}

Ideally I wouldn't want the slider to even have an option of '0', and to have the default set to 5.

Kevin
  • 1,189
  • 2
  • 17
  • 44

2 Answers2

1

If you are leaving the view controller, it's almost certainly true that you are destroying it, and when you are trying to go back, it's being created again. If you are using segues for example, this would be the case.

Unless you are using a navigation controller and pushing the view controller on it, the view controller will be created again from scratch, therefore setting the value to the slider that is set in the storyboard.

On another topic, it's not really a good practice to have code such as the distanceMap outside of the class, it may lead to strange behaviour. The best practice is to have that value inside of the class.

In order to achieve what you are trying, you should send the value of the slide though your VCs, or store it somewhere else. The first option is really not elegant, so the second would be the best.

For example, store it in a singleton that lives through the app life, and you could ask for the value to the singleton whenever this VC appears. Whenever the user changes the value in the slider, you set it in the singleton. Another solution is to store it in the database, but that looks way to overkill.

Federico Ojeda
  • 758
  • 7
  • 11
0

I've managed to find a way such that I don't need to have the 0 in the slider.

//initialize the global variable that way this is the first thing the slider will change to
var distanceMap: Int = 5


override func viewDidAppear(_ animated: Bool) {

    rangeSlider.value = Float(distanceMap)
    distanceLabel.text = String(Int(rangeSlider.value))

}

The slider value will now be 5 the first time, and the variable can be changed and updated fine.

Kevin
  • 1,189
  • 2
  • 17
  • 44
  • You do realize when the user restarts the app and ViewDidLoad triggers again the slider value will default back to 5. I'm requiring a similar global variable to store and use throughout my app for a UISlider. – David Henry Aug 20 '17 at 06:55