I'm trying to create an app which will have an option to customize button colors and have the colors set as a default until changed by the user.
I've researched using UserDefaults
and keys, and I've tried to implement them into my code. Even though I don't get an error, the data resets when changing storyboards even though I've set the values to the key data.
Any help or advice would be appreciated since this is my first ever Xcode/Swift project. Also, if someone would be so kind as to explain how I would set button colors in other storyboards as well through the aforementioned options, I would really appreciate it.
//
// SettingsViewController.swift
// Calculator++
//
// Created by School on 12/4/16.
// Copyright © 2016 BlueFireStudios. All rights reserved.
//
import UIKit
class SettingsViewController: UIViewController {
//Setting default values
var defaults = UserDefaults.standard
//Setting slider starting positions
//Digit slider defaults
var redValue1: CGFloat = CGFloat(UserDefaults.standard.integer(forKey: "red"))
var greenValue1: CGFloat = CGFloat(UserDefaults.standard.integer(forKey:"green"))
var blueValue1: CGFloat = CGFloat(UserDefaults.standard.integer(forKey: "blue"))
//Outlet for display button labeled "4"
@IBOutlet weak var digitdisp: UIButton!
var myInt = Int()
//Displays digit color settings
func displayColors1() {
digitdisp.backgroundColor = UIColor(red: redValue1, green: greenValue1, blue: blueValue1, alpha: 1.0)
defaults.set(NSKeyedArchiver.archivedData(withRootObject: UIColor.red),
forKey:"red")
defaults.set(NSKeyedArchiver.archivedData(withRootObject: UIColor.blue), forKey:"blue")
defaults.set(NSKeyedArchiver.archivedData(withRootObject: UIColor.green), forKey:"green")
}
//Setting R color in digit RGB set (red in func displayColors1)
@IBAction func redslider1(_ sender: UISlider) {
redValue1 = CGFloat(sender.value)
defaults.set(NSKeyedArchiver.archivedData(withRootObject: UIColor.red, forKey:"red")
displayColors1()
}
//Setting G color in digit RGB set (green in func displayColors1)
@IBAction func greenslider1(_ sender: UISlider) {
greenValue1 = CGFloat(sender.value)
defaults.set(NSKeyedArchiver.archivedData(withRootObject: UIColor.green), forKey:"green")
displayColors1()
}
//Setting B color in digit RGB set (blue in func displayColors1)
@IBAction func blueslider1(_ sender: UISlider) {
blueValue1 = CGFloat(sender.value)
defaults.set(NSKeyedArchiver.archivedData(withRootObject: UIColor.blue), forKey:"blue")
displayColors1()
}
override func viewDidLoad() {
super.viewDidLoad()
myInt = 50
displayColors1()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}