I am trying to change the backgroundColor of a few UIButtons based on a dictionary that I have saved the colors in String.
ie
button1.backgroundColor = dic!.valueForKey("Button1Color") as? UIColor
// in the dictionary:
Button1Color = "UIColor.redColor()"
i get nil. Why?
You could store the color as a string and use some library or code snippet (eg here ) to convert it to UIColor
and back to string.
Alternatively, you could store the 4 Float
numbers that make up a UIColor in a scheme similar to this:
let bg1: [String:CGFloat] = ["red": 1, "green": 0, "blue": 0, "alpha": 1]
and use the initializer UIColor(red:green:blue:alpha)
to create a UIColor
object.
NB: These are some ideas for persisting the data to disk. Obviously, in an in-memory only array you can put any object you like, including UIColor
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.