Retain values when switching view controllers

Hello,
My app has a settings page where the user can toggle buttons etc and I would like those values to carry over to the next screen / view controller file so that I can display different things depending on what the user inputted. I have tried everything from state and binding variables to cocoa touch class with a segue that can modify the variables of the destination view controller, but I always get errors. I'm not sure if it's because I'm using a more recent version of swift? But here is my code and some of the things I've tried:

Settings page:

Code Block
import UIKit
class SessionSetupViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
//@State var visualBool = true
//var feedbackAmt: String = "String"
//var feedbackAmt = String()
@IBAction func VisualToggle(_ sender: Any) {
}
@IBAction func AuditoryToggle(_ sender: Any) {
}
@IBOutlet weak var PickerLabel: UILabel!
@IBOutlet weak var FeedbkAmtPicker: UIPickerView!
let amounts = ["Low", "Medium", "High"]
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return amounts[row]
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return amounts.count
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
PickerLabel.text = amounts[row]
//feedbackAmt = amounts[row]
//let feedbackAmt = amounts[row]
}
@IBAction func BeginButtonPressed(_ sender: Any) {
self.performSegue(withIdentifier: "BeginSegue", sender: self)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
// override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// if segue.identifier == "BeginSegue" {
// let destinationController = segue.destination as! BluetoothViewController
// destinationController.feedbackAmt = PickerLabel.text!
// }
// }
}


And then the destination view controller:

Code Block
import UIKit
import CoreBluetooth
//local central device object
class BluetoothViewController: UIViewController, CBPeripheralDelegate, CBCentralManagerDelegate {
//properties
private var centralManager: CBCentralManager!
private var peripheral: CBPeripheral!
var feedbackAmt = String() // from session setup view controller
//var sessionSetupViewController: SessionSetupViewController?
//let bluetoothViewController = BluetoothViewController()
//let sessionSetupViewController = SessionSetupViewController()
//bluetoothViewController.sessionSetupViewController = sessionSetupViewController
//sessionSetupViewControler?.feedbackAmt
//label to display stage of gait cycle
@IBOutlet weak var StateLabel: UILabel!
//characteristics
private var StateVar: CBCharacteristic?
override func viewDidLoad() {
super.viewDidLoad()
centralManager = CBCentralManager(delegate: self, queue: nil)
//StateLabel.text = "\(ArduinoPeripheral.ArduinoCharacteristicUUID)"
//StateLabel.text = "\(SessionSetupViewController.feedbackAmt)"
//StateLabel.text = feedbackAmt
}
// core bluetooth functions...


Please let me know if there is another way I can pass those values/settings to the next view controller. Thanks!


A very good way is to save in UserSettings.
And reload settings in each view that need it in viewWillAppear.

It is simpler than sending explicitely the values to a lot of views…
Retain values when switching view controllers
 
 
Q