我正在嘗試創建一個應用程序,該應用程序可以選擇自定義按鈕顏色並將顏色設置爲默認值,直到用戶更改。保存並調用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.
}
*/
}
這將是人們更容易地診斷,如果你簡化你的代碼到重現該問題所需的最少的代碼。也就是說,重寫它只包含一組顏色,一個按鈕,一組滑塊。測試它,看看問題是否仍然存在,然後發佈該代碼。 –
您可以通過保存UIColor而不是3個單獨的CGFloats來降低一些代碼複雜性。爲什麼不這樣做:'defaults.set(signdisp.backgroundColor,forKey:「myFavoriteColor」)'? –
@Joe Daniels你真的可以直接使用UIColor對象嗎? NSUserDefaults文檔建議它只能(直接)存儲屬性列表數據類型(請參閱https://developer.apple.com/reference/foundation/userdefaults)。對於其他數據類型的常見變通方法,特別是UIColor,請參閱http://stackoverflow.com/questions/1275662/saving-uicolor-to-and-loading-from-nsuserdefaults –