How do I get a value from a variable in another View Controller

I'm trying to get the values from 4 different variables in PrefsViewController in ViewController but when PrefsViewController is dismissed the values reset.

Snippet of ViewController.swift

class ViewController: NSViewController, NSWindowDelegate { 
    
    var triesEnabled = false
    var minNumber = 0
    var maxNumber = 20
    var maxTries = 3
    
    @objc func applyPrefs() {
        let mainSB = NSStoryboard(name: "Main", bundle: nil)
        let prefsVC: PrefsViewController = mainSB.instantiateController(withIdentifier: "prefsViewController") as! PrefsViewController
        minNumber = prefsVC.prefsMinNum
        maxNumber = prefsVC.prefsMaxNum
        triesEnabled = prefsVC.prefsTriesEnabled
        maxTries = prefsVC.prefsMaxTries
        print("\(minNumber) \(maxNumber) \(triesEnabled) \(maxTries)") // here I can see that it has been reset to the default values
        resetGame(minRange: minNumber, maxRange: maxNumber, isTriesEnabled: triesEnabled, triesLimit: maxTries)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(self, selector: #selector(applyPrefs), name: Notification.Name("notifyApplyPrefs"), object: nil )
        resetGame(minRange: minNumber, maxRange: maxNumber, isTriesEnabled: triesEnabled, triesLimit: maxTries)
    }
}

Snippet of PrefsViewController

class PrefsViewController: NSViewController {
    
    var triesOn = false
    
    var prefsMinNum: Int = 0
    var prefsMaxNum: Int = 20
    var prefsTriesEnabled: Bool = false
    var prefsMaxTries: Int = 3
    
    @IBAction func closeButton(_ sender: Any) {
        if (!minNumTextField.stringValue.isInt || !maxNumTextField.stringValue.isInt || !triesTextField.stringValue.isInt) {
            let alert = NSAlert()
            alert.messageText = "Error"
            alert.informativeText = "You can only enter (whole) numbers in the text fields."
            alert.alertStyle = .warning
            alert.addButton(withTitle: "OK")
            alert.runModal()
            return
        }
        prefsMinNum = Int(minNumTextField.intValue)
        prefsMaxNum = Int(maxNumTextField.intValue)
        prefsTriesEnabled = triesOn
        prefsMaxTries = Int(minNumTextField.intValue)
        
        print("minNumTextField: \(prefsMinNum) maxNumTextField: \(prefsMaxNum) triesCheckBox: \(prefsTriesEnabled) triesTextField: \(prefsMaxTries)")
        NotificationCenter.default.post(name: Notification.Name("notifyApplyPrefs"), object: nil)
        self.dismiss(self)
    }
}

The prefs view is shown as a sheet (I don't know if that's important)



Read more here: https://stackoverflow.com/questions/64188699/how-do-i-get-a-value-from-a-variable-in-another-view-controller

Content Attribution

This content was originally published by YaYPIXXO at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: