0

I want to take user settings details from this view controller and read these details to the previous view controller. I have tried many different ways, but I cannot take values until I visit this view controller

I have tried first method from this page Pass Data Tutorial

This method is also not working. I think it is very simple, but I cannot figure out the right way to do it.

class SetConvViewController: UIViewController {

    var engS = "engS"

    @IBOutlet weak var swithEnglish: UISwitch!

    override func viewDidLoad() {
        super.viewDidLoad()

        if let eng2 = defaults.value(forKey: engS)
        {
            swithEnglish.isOn = eng2 as! Bool
        }

    }

    let defaults = UserDefaults.standard

    @IBAction func switchEng(_ sender: UISwitch) {
        defaults.set(sender.isOn, forKey: engS)

    }
}
2
  • The key to answering your question (if I read it correctly) is for you to detail how the two view controllers are tied together. Is one presenting the other? Is there a segue involved? Show us that code and we can probably help you. Commented May 30, 2018 at 23:18
  • Which part is not working? The code you provided looks quite fine. Can you show how you are trying to read this value? Commented May 31, 2018 at 0:13

3 Answers 3

1

If I understand you correctly from this part - „but I cannot take values until I visit this view controller” - your problem lies with the fact, that until you visit your settings, there is no value for them in UserDefaults. If you are reading them using getObject(forKey:) method, I’d recommend you to switch to using getBool(forKey:), since it will return false even if the value has not been set yet for that key ( docs )

Anyhow, if you want to set some default/initial values you can do so in your didFinishLaunching method in AppDelegate :

if UserDefaults.standard.object(forKey: „engS”) == nil {
    // the value has not been set yet, assign a default value
}

I’ve also noticed in your code that you used value(forKey:) - you should not do that on UserDefaults - this is an excellent answer as to why - What is the difference between object(forKey:) and value(forKey:) in UserDefaults?.

On a side note, if you are using a class from iOS SDK for the first time, I highly recommend looking through its docs - they are well written and will provide you with general understanding as to what is possible.

Sign up to request clarification or add additional context in comments.

Comments

0

I would recommend you to store this kind of data as a static field in some object to be able to read it from any place. e.g.

class AppController{
    static var userDefaults = UserDefaults.standard
}

and then you can save it in your SetConvViewController like

@IBAction func switchEng(_ sender: UISwitch) {
    AppController.userDefaults.set(sender.isOn, forKey: engS)
}

and after that you can just read it from any other view controller just by calling

AppController.userDefaults

3 Comments

UserDefaults.standard is already such a static object. Setting it to some other variable is not needed.
That obviously was for example purpose, author can use any other value, I just shared the idea. He wouldn't need to pass UserDefaults.standard to different ViewController
Ah, but a good example makes all the difference. Especially when you can clearly see what OP is trying to pass.
0

Using segues you can set to any destination whether it be next vc or previous:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        if segue.identifier == "PreviousVC" {
            if let prevVC = segue.destination as? PreviousViewController {

                //Your previous vc should have your storage variable.
                prevVC.value = self.value

            }
    }

If you're presenting the view controller:

Destination vc:

//If using storyboard...
let destVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DestinationViewController") as! DestinationViewController

destVC.value = self.value
self.present(destVC, animated: true, completion: nil)

Previous vc:

weak var prevVC = self.presentingViewController as? PreviousViewController

if let prevVC = prevVC {

    prevVC.value = self.value
}

2 Comments

OP specifically said that he wants to pass the data the other way - not from the presenting VC into the presented, but back from the presented into the presenting.
Yeah, misunderstood the question. Fixed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.