0

I have a segmented controller that I click, it then changes 6 labels at once with a different value (metric to imperial).

I have that working but when i change to a different page and come back the selector resets to position 0. is there a way to save the position selected when i leave the controller and comeback. I say controller but its only one controller with many nib files that run like a book so when i turn the page and go back it is back in the original position.

I tried this code in may view did appear but no luck.

if measurementSwitch == 0 {
    metricImperialSwitch?.selectedSegmentIndex = 0
         print(measurementSwitch)
    }else {
        metricImperialSwitch?.selectedSegmentIndex = 1
        print(measurementSwitch)
    }

and here is the function.

func P18Switch() {

        if metricImperialSwitch.selectedSegmentIndex == 0
        {
            measurementSwitch = 0
            CWLabel.text = "kg"; TWLabel.text = "kg"; CWaLabel.text = "cm"; TWaLabel.text = "cm"; CHLabel.text = "cm"; THLabel.text = "cm"
        }
        else if metricImperialSwitch.selectedSegmentIndex == 1
        {
            measurementSwitch = 1
            CWLabel.text = "st"; TWLabel.text = "st"; CWaLabel.text = "inch"; TWaLabel.text = "inch"; CHLabel.text = "inch"; THLabel.text = "inch"

        }
}

and then calling the function in view did load.

Tony Merritt
  • 1,177
  • 11
  • 35

1 Answers1

0

You can save the selected segment index with UserDefaults:

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    UserDefaults.standard.set(metricImperialSwitch?.selectedSegmentIndex, "selectedIndex")
}



override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    if UserDefaults.standard.value(forKey: "selectedIndex") != nil{
         measurementSwitch = UserDefaults.standard.value(forKey: "selectedIndex") as! Int
    }else{
         measurementSwitch = 0
    }
    if measurementSwitch == 0 {
         metricImperialSwitch?.selectedSegmentIndex = 0
    }
    else {
        metricImperialSwitch?.selectedSegmentIndex = 1
    }
    self.P18Switch()
}

An alternative to UserDefaults would be using Global Variables. Both of the methods work just fine, but it is better to use UserDefaults, because the values are saved even if the app is closed completely.

Community
  • 1
  • 1
Mr. Xcoder
  • 4,719
  • 5
  • 26
  • 44
  • Hi, Thank you for your reply, I tried the code but get the same result, i put in a print function to see if it was getting going in to the code and the print did not come back so i am wondering if it is calling it? – Tony Merritt Feb 07 '17 at 19:04
  • Try moving it to viewDidLoad, see what you get. If print does not work, it is not called. – Mr. Xcoder Feb 07 '17 at 19:04
  • Please try the updated code, I forgot **super** and **animated** – Mr. Xcoder Feb 07 '17 at 19:08
  • Cheers for the reply again, so this time I get to the page and I added a Print to this part of the code..... if UserDefaults.standard.value(forKey: "selectedIndex") != nil{ measurementSwitch = UserDefaults.standard.value(forKey: "selectedIndex") as! Int print("Got here yes")........I get the print reply back when i i go to the page and return but the switch still resets to original position – Tony Merritt Feb 07 '17 at 21:58
  • Are you calling your actual function? – Mr. Xcoder Feb 07 '17 at 22:00
  • Tomorrow, I will update the answer with another approach, but currently I don't have access to the computer. Make sure to come back and check the updated answer – Mr. Xcoder Feb 07 '17 at 22:03
  • Im the function, p18Switch() i also did this print UserDefaults.standard.value(forKey: "selectedIndex") != nil{ measurementSwitch = UserDefaults.standard.value(forKey: "selectedIndex") as! Int print("Got here yes") print("yes\(measurementSwitch)") when I select index 1 and leave then come back to the page it prints yes 1 but the switch goes back to index 0 then when i leave and return again it shows yes 0 so something is sets it back to 0 once i come back to the page, when i leave the page it is keeping the value i think its just loosing it when i return – Tony Merritt Feb 07 '17 at 22:37
  • I solved it, thanks to your help, I put the code in view will appear that way the code is called before the view appears and then it can set the values its stored. Thank you so much for the help on this, I am truly grateful. – Tony Merritt Feb 07 '17 at 22:47
  • You're welcome! If you have any other question just ask – Mr. Xcoder Feb 08 '17 at 06:03