保存并调用UIColor函数

时间:2016-12-05 23:57:03

标签: ios swift3 nsuserdefaults uicolor

我正在尝试创建一个应用程序,该应用程序可以自定义按钮颜色,并将颜色设置为默认值,直到用户更改为止。

我研究过使用UserDefaults和密钥,我试图将它们应用到我的代码中。即使我没有收到错误,即使我已将值设置为关键数据,数据也会在更改故事板时重置。

任何帮助或建议都会受到赞赏,因为这是我的第一个Xcode / Swift项目。此外,如果有人能够解释如何通过上述选项在其他故事板中设置按钮颜色,我会非常感激。

//
//  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.
}
*/

}

2 个答案:

答案 0 :(得分:0)

对于您的一个颜色/按钮/滑块集,您将三个值保存为您从未读过的默认值并在任何地方使用:

defaults.set(CGFloat(redValue2), forKey: "SetRed2")
defaults.set(CGFloat(greenValue2), forKey: "SetGreen2")
defaults.set(CGFloat(blueValue2), forKey: "SetBlue2")

您从默认值中读取的唯一值是您的其他完全不相关的键集:

var redValue1: CGFloat = CGFloat(UserDefaults.standard.integer(forKey: "SetRed1"))
var greenValue1: CGFloat = CGFloat(UserDefaults.standard.integer(forKey: "SetGreen1"))
var blueValue1: CGFloat = CGFloat(UserDefaults.standard.integer(forKey: "SetBlue1"))

即,您使用以字符1结尾的键从默认值中读取(和写入),但仅使用以字符2结尾的键将(不读取)写入默认值。

答案 1 :(得分:0)

要保存它:

long listID = Long.parseLong(total_json.getString("listID"));

要检索:

var red: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0
var alpha: CGFloat = 0

UserDefaults.standard.set([red, green, blue, alpha], forKey: "textColor")