Jacob
Jacob

Reputation: 57

Cannot take values from other view controller Swift

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)

    }
}

Upvotes: 0

Views: 101

Answers (3)

u84six
u84six

Reputation: 4941

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
}

Upvotes: 0

Losiowaty
Losiowaty

Reputation: 8006

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.

Upvotes: 1

Farkhad Gojazadeh
Farkhad Gojazadeh

Reputation: 96

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

Upvotes: 0

Related Questions