2016-12-05 55 views
0

我正在嘗試創建一個應用程序,該應用程序可以選擇自定義按鈕顏色並將顏色設置爲默認值,直到用戶更改。保存並調用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

您可以通過保存UIColor而不是3個單獨的CGFloats來降低一些代碼複雜性。爲什麼不這樣做:'defaults.set(signdisp.backgroundColor,forKey:「myFavoriteColor」)'? –

+0

@Joe Daniels你真的可以直接使用UIColor對象嗎? NSUserDefaults文檔建議它只能(直接)存儲屬性列表數據類型(請參閱https://developer.apple.com/reference/foundation/userdefaults)。對於其他數據類型的常見變通方法,特別是UIColor,請參閱http://stackoverflow.com/questions/1275662/saving-uicolor-to-and-loading-from-nsuserdefaults –

回答

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結尾的密鑰寫入(不讀取)默認值。

0

要保存它:

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") 

要檢索:

if let textColor = prefs.array(forKey: "textColor") { 
     lblFirst.textColor = UIColor(red: textColor[0] as! CGFloat, green: textColor[1] as! CGFloat, blue: textColor[2] as! CGFloat, alpha: textColor[3] as! CGFloat) 
    }