how to call UserDefaults from another view controller

I have 2 viewcontrollers, one displays the UI and the 2nd are settings. Im using the below code to save a segmented control button:


UserDefaults.standard.set(selectorLabel.selectedSegmentIndex, forKey: "stateSelected")


I then call it by:


if let value = UserDefaults.standard.value(forKey: "stateSelected"){
            let selectedIndex = value as! Int
            selectorLabel.selectedSegmentIndex = selectedIndex
        }


This works as intended.


How would i save the actual title of each button press? For instance the one side of the control is labeled "LBs & INs" and the other "KGs & CMs". I want to save which one is selected. I then want to be able to use that saved text value on a label on the first viewcontroller when the view loads

Replies

Not sure I understand what the problem is (except how to use user defaults).


The principle is simple:


To read user defaults (if a String as in your case) in myString:


        let defaults = UserDefaults.standard
        var myString = defaults.string(forKey: "Some key")


To save myString in defaults

            defaults.set(myString, forKey: "Some key")

I only want to call a key if the segmented control index is 0. So how would my if statement look if i saved a string to userdefaults and i saved the segmented control selected index to userdefaults.


Basically, if selectedstateindex = 0 then label.text = key1 else if selectedstateindex = 1 then label.text = key2

Not very clear.


Let's go step by step.


I only want to call a key if the segmented control index is 0.

Do you mean when you select index 0 in segmented control ?

Or do you mean, selectedindex is 0 when you leave the view ?


What is not clear either is what you want to save from segmentedcontrol. Only the selected index

Where is segmented control located ? UI or settings ?


So explain more, and explain where the different parts (objects, code) are: UI or settings.


And how you transition between the 2.

First off, thank you for taking the time to help me. ill try to make this as clear as i possibly can 🙂


I have two ViewControllers; one is a user interface with a few labels, textboxes, pickerviews and buttons. On that VC i use a segue to go into the second viewcontroller to change 1 setting using a segmented control and a back button using a segue to go back. That segmented control is basically "LBs & INs" and "KGs & CMs" index values of 0 and 1. Im already able to save the segment controller state on that view with no problem - works every time.


What i now want to do is on the first viewcontroller with the labels, is change a label IF the saved segment control index is 0 or 1. So in my head, i would add an if statement to the viewdidload possibly - if the userdefault segment control selected state index is 0 set the label.text to the segment control title or i could hardcode it here. If segment control state index is 0, then label.text = LBs & INs" else if index is 1 then label.text = "KGs & CMs".


Basically when a user loads my app, a label is dependent on what the segment control index is saved as on the settings VC.