I've declared a boolean with default value true, if my UISwitch is on I want boolean variable to be true else false, my code works fine until I try to store that boolean in UserDefaults when I reset the xCode Simulator I check with print method but it is not really saved...
any solution will be appericated.
var userDefaultSamarxvo:Bool = true
let defaults = UserDefaults.standard
override func viewDidLoad() {
super.viewDidLoad()
defaults.set(userDefaultSamarxvo,forKey: "Samarxvo") // set of userdefault
if userDefaultSamarxvo {
print("hello")
}else{
print("olleh")
}
}
#IBAction func samarxvoDidChange(_ sender: UISwitch) {
if sender.isOn {
userDefaultSamarxvo.toggle()
print("samarxo")
}else{
userDefaultSamarxvo.toggle()
print("samarxo 1 ")
}
}
In viewDidLoad you want to load the saved value from user defaults. When the switch changes you need to save the new value to user defaults.
Setting a local variable (userDefaultSamarxvo) doesn't change what is stored in the user defaults; Local variables don't bind to user defaults storage.
There is an added complication; bool(forKey) will return false if there is no value for the key in UserDefaults. If you want the initial value to be true then you need to handle that in some way. You can use object(forKey) which returns an optional
var userDefaultSamarxvo:Bool = true {
didSet {
UserDefaults.standard.set(userDefaultSamarxvo, forKey:switchKey)
}
}
let defaults = UserDefaults.standard
let switchKey = "Samarxvo"
#IBOutlet weak var theSwitch: UISwitch!
override func viewDidLoad() {
super.viewDidLoad()
if defaults.object(forKey: switchKey) != nil {
userDefaultSamarxvo = defaults.bool(forKey: switchKey)
}
self.theSwitch.isOn = userDefaultSamarxvo
}
#IBAction func samarxvoDidChange(_ sender: UISwitch) {
userDefaultSamarxvo = sender.isOn
}
I have used a didSet clause to update the user defaults value when the property changes.
Related
This question already has answers here:
How do i keep UISwitch state when changing ViewControllers?
(3 answers)
Closed 5 years ago.
I would like to save the State of an UISwitch after to change between View Controllers. Any help would be greatly appreciated!
I have a first View Controller with an UISwitch, to control the music in the background in different View Controllers:
#IBOutlet weak var SwitchMusic: UISwitch!
let defaults = UserDefaults.standard
var switchON : Bool = false
#IBAction func checkState(_ sender: UISwitch) {
if (sender.isOn == true)
{
switchON = true
defaults.set(switchON, forKey: "switchON")
MusicHelper.sharedHelper.playBackgroundMusic()
}
if (sender.isOn == false)
{
switchON = false
defaults.set(switchON, forKey: "switchON")
MusicHelper.sharedHelper.stopBackgroundMusic()
}
}
And a Second View Controller to load or no the music in the background if the switch is On or Off:
override func viewDidLoad() {
super.viewDidLoad()
if defaults.value(forKey: "switchON") != nil{
let switchON: Bool = defaults.value(forKey: "switchON") as! Bool
if switchON == true{
MusicHelper.sharedHelper.playBackgroundMusic()
}
else if switchON == false{
MusicHelper.sharedHelper.stopBackgroundMusic()
}
}
}
Also I have a class with the music:
class MusicHelper {
let defaults = UserDefaults.standard
static let sharedHelper = MusicHelper()
var musicBackgroundIntro:AVAudioPlayer = AVAudioPlayer()
func playBackgroundMusic() {
do {
let audioPath = Bundle.main.path(forResource: "Music", ofType: "mp3")
try musicBackgroundIntro = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!) as URL)
musicBackgroundIntro.numberOfLoops = -1
musicBackgroundIntro.prepareToPlay()
musicBackgroundIntro.play()
} catch {
print("Cannot play the file")
}
}
func stopBackgroundMusic() {
musicBackgroundIntro.stop()
}
}
Now it is working perfectly the music in the background between View Controllers, and it is possible to turn off and on... but unfortunately do not save the current state of the UISwitch, and always when I enter in the First View Controller the state of the Switch is On.
Also any idea that how will be possible to apply in a Slider too?
Any help would be greatly appreciated!
Thanks
The easiest way for you would be to create a static var isSwitchOn: Bool = false
This state will be preserved between back and forth transitions.
You should reflect the state, if music is playing...
class MusicHelper {
public isPlaying: Bool {
get {
return musicBackgroundIntro.isPlaying
}
}
// your stuff here..
}
That way in other view controllers:
SwitchMusic.isOn = MusicHelper.sharedHelper.isPlaying
If you need other view controllers to update in response to this, you can add a delegate event (aka observer) if necessary.
Try something like that: Use the UISwitch as an #IBOutlet.
#IBOutlet weak var checkState: UISwitch!
override func viewDidLoad() {
super.viewDidLoad()
self.checkState.addTarget(self, action: #selector(action(sender:)), for: .valueChanged)
}
// Save state
func action(sender: UISwitch) {
let userDefaults = UserDefaults.standard
userDefaults.set(sender.isOn, forKey:"identifier")
}
// Retrieve state
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let userDefaults = UserDefaults.standard.bool(forKey: "identifier")
self.checkState.setOn(userDefaults, animated: false)
}
You can give the switch a default value when it's created in viewcontroller1.
Assign the (default.value(forKey:"switchOn") as! Bool ) ?? false to that switch.
I am making a feature in my app that allows you to mute the music, i put a switch there so you can change it between on and off. However, the switch works fine in the app, when i close the vie controller and i pull it back up it will still show it how it was left, but when you close the app and open it back up the switch looks as if it off but it still plays the music.This is my code and i will attach a picture.
import Foundation
import UIKit
import SpriteKit
import AVFoundation
var bombSoundEffect: AVAudioPlayer!
var Ghost = SKSpriteNode()
class SecondViewController: UIViewController {
var sw = false
#IBOutlet var mySwitch: UISwitch!
#IBAction func switchpressed(_ sender: AnyObject) {
let defaults = UserDefaults.standard
if mySwitch.isOn{
defaults.set(true, forKey: "SwitchState")
if bombSoundEffect != nil {
bombSoundEffect.stop()
bombSoundEffect = nil
}
}
else{
defaults.set(false, forKey: "SwitchState")
let path = Bundle.main.path(forResource: "newmusic.wav", ofType:nil)!
let url = URL(fileURLWithPath: path)
do {
let sound = try AVAudioPlayer(contentsOf: url)
bombSoundEffect = sound
sound.numberOfLoops = -1
sound.play()
} catch {
// couldn't load file :(
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
super.viewDidLoad()
let defaults = UserDefaults.standard
if (defaults.object(forKey: "SwitchState") != nil) {
mySwitch.isOn = defaults.bool(forKey: "SwitchState")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
}
and here is the picture
If you didn't understand what i said above please ask questions.
As i understand from your code, you are not checking the User Defaults properly. In your viewDidLoad you have to check the bool value of your user defaults is set to true or not.
override func viewDidLoad() {
super.viewDidLoad()
let defaults = UserDefaults.standard
//check bool Value is set to true or not
if defaults.bool(forKey: "SwitchState") == true {
mySwitch.isOn = defaults.bool(forKey: "SwitchState")
} else {
print("false")
}
}
Please check this code. Hopefully it will helps you.
In viewDidLoad you need to check for when you need to pause the music:
// Keep this part the same
let defaults = UserDefaults.standard
if (defaults.object(forKey: "SwitchState") != nil) {
mySwitch.isOn = defaults.bool(forKey: "SwitchState")
}
// Check to see if the switch is off, if so, stop the music
if !mySwitch.isOn {
bombSoundEffect.stop()
bombSoundEffect = nil
}
You can also take out the duplicate call to super.viewDidLoad.
Note: It really looks like there is more code in your view controller, because there isn't anything currently there that would be playing the music, so this solution may not work given that you haven't provided enough information.
I want to check the State of an UISwitch from another View Controller. And if the switch is on, then I want the backgroundColor of the View to be red, otherwise it should be green. I tried to use UserDefaults, but I have no idea what to start with. Any help would be greatly appreciated!
NOTE: THIS IS THE VERSION FOR SWIFT 3:
First of all, I've set the initial state of the switch to off. I have added a button, which leads us to the secondViewController and also checks the state of the switch. Here is my code(for the View Controller which contains the switch):
#IBOutlet weak var switch1: UISwitch!
let defaults = UserDefaults.standard
var switchON : Bool = false
#IBAction func checkState(_ sender: AnyObject) {
if switch1.isOn{
switchON = true
defaults.set(switchON, forKey: "switchON")
}
if switch1.isOn == false{
switchON = false
defaults.set(switchON, forKey: "switchON")
}
}
And for the secondView:
let defaults = UserDefaults.standard
override func viewDidLoad() {
super.viewDidLoad()
if defaults.value(forKey: "switchON") != nil{
let switchON: Bool = defaults.value(forKey: "switchON") as! Bool
if switchON == true{
self.view.backgroundColor = UIColor.red()
}
else if switchON == false{
self.view.backgroundColor = UIColor.green()
}
}
}
I want to perform
func performSegue() {
self.performSegueWithIdentifier("toTabSegue", sender: self)
}
immediately when
var isUpdated:Bool = false
is true.
How do I set something in viewdidload so when isUpdated changes to true it performs a segue?
You can use a didSet observer:
var isUpdated = false {
didSet {
if isUpdated && !oldValue {
performSegue()
}
}
}
Note that Swift provides oldValue in didSet automatically.
I have a UISwitch in the settings menu in my app and I am having difficulties trying to get the initial state to be 'on'. Even if I set the initial state to be 'on' in the attributes inspector, it still sets it to 'off' when the is opened for the first time.
Basically at the moment, the switch will be set to 'off' when the app opened for the first time however it does save the state if you change it so that works fine.
Here is my code:
#IBAction func dupOffOnSwitch(sender: AnyObject) {
if dupSwitch.on == true {
autoAdjust = true
println(autoAdjust)
} else {
autoAdjust = false
println(autoAdjust)
}
override func viewWillAppear(animated: Bool) {
dupSwitch.on = NSUserDefaults.standardUserDefaults().boolForKey("autoAdjustSettings")
println(NSUserDefaults.standardUserDefaults().boolForKey("autoAdjustSettings"))
}
'autoAdjust' is declared under import UIKit as true.
In order to set NSUserDefaults.standardUserDefaults().boolForKey("autoAdjustSettings") to true for the first time you ever launch your app, you can replace the application:didFinishLaunchingWithOptions: method of your AppDelegate class with this code:
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
//If app has never been launched...
if !NSUserDefaults.standardUserDefaults().boolForKey("isNotFirstLaunch") {
//Set autoAdjustSettings and isNotFirstLaunch to true
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "autoAdjustSettings")
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "isNotFirstLaunch")
//Sync NSUserDefaults
NSUserDefaults.standardUserDefaults().synchronize()
}
return true
}
Of course, you can do the previous code in your UIViewController subclass but the AppDelegate should be the place for those settings.
Once done, your UIViewController subclass should look like this:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var dupSwitch: UISwitch!
#IBAction func dupOffOnSwitch(sender: AnyObject) {
dupSwitch.on = (sender as UISwitch).on //Bool
NSUserDefaults.standardUserDefaults().setBool(dupSwitch.on, forKey: "autoAdjustSettings")
NSUserDefaults.standardUserDefaults().synchronize()
}
override func viewDidLoad() {
super.viewDidLoad()
dupSwitch.on = NSUserDefaults.standardUserDefaults().boolForKey("autoAdjustSettings")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Unless you have a good reason to, I don't think you need to use the viewWillAppear: method in your UIViewController subclass for your NSUserDefaults statements. viewDidLoad: should be the right place for that.
I fixed my issue by adding this:
dupSwitch.on = NSUserDefaults.standardUserDefaults().boolForKey("autoAdjustSettings")
println(NSUserDefaults.standardUserDefaults().boolForKey("autoAdjustSettings"))
userReturnedAuto = NSUserDefaults.standardUserDefaults().boolForKey("userReturnedAuto")
if userReturnedAuto == false {
dupSwitch.on = true
userReturnedAuto = true
NSUserDefaults.standardUserDefaults().setBool(userReturnedAuto, forKey: "userReturnedAuto")
NSUserDefaults.standardUserDefaults().setBool(userReturnedAuto, forKey: "autoAdjustSettings")
}
Probably not the ideal solution but it works.