I have created a custom table view cell. In my app, there are two files. I have a separate file for the custom table view cell. Then I have a tableviewcontroller file for the table as a whole.
On my custom table view cell, there is a UISwitch. The initial value of this UISwitch is off. In the file for the custom table view cell, I have created a function for the switch. I have also created an outlet for the switch.
@IBOutlet var theSwitch: UISwitch!
@IBAction func mySwitch(_ sender: AnyObject) {
}
In my table view controller, I have already created the view did appear function. I am assuming what I need to do is save the value of the switch in
@IBAction func mySwitch(_ sender: AnyObject) {
}
Then in the table view controller, somehow call it in this view did appear function.
override func viewDidAppear(_ animated: Bool) {
}
What I am having trouble with is saving the value of the switch if it is changed. For example, if a user opens the app and turns the switch on, I want the app to save it, so that when the app is reopened, the value of the switch is still on. Hopefully, this can be done by using User Defaults. If someone can show me the code to do this, that would be great.
You can check value like this:
Swift 2
and for saving value to userDefaults:
Swift 3
and set value
hope this helps