Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Chris Wade
9,574 PointsHow to get colours to match in Build a Simple iOS app.
Im in the final stages of the build a simple iOS app using swift and I'm having trouble with getting my button to match the color of my background every time the fact changes.
import UIKit
class ViewController: UIViewController { @IBOutlet weak var funFactLabel: UILabel! @IBOutlet weak var funFactButton: UIButton!
let factBook = FactBook()
let colorWheel = ColorWheel()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
funFactLabel.text = factBook.randomFact()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func showFunFact() {
let randomColor = colorWheel.randomColor()
view.backgroundColor = randomColor // <---this line has a warning Variable 'randomColor' was never mutated, consider changing to let.
funFactButton.tintColor = randomColor
funFactLabel.text = factBook.randomFact()
}
}
1 Answer
Chris Wade
9,574 Pointsimport Foundation import UIKit
struct ColorWheel {
let colorsArray = [
UIColor(red: 90/255.0, green: 187/255.0, blue: 181/255.0, alpha: 1.0), //teal color
UIColor(red: 222/255.0, green: 171/255.0, blue: 66/255.0, alpha: 1.0), //yellow color
UIColor(red: 223/255.0, green: 86/255.0, blue: 94/255.0, alpha: 1.0), //red color
UIColor(red: 239/255.0, green: 130/255.0, blue: 100/255.0, alpha: 1.0), //orange color
UIColor(red: 77/255.0, green: 75/255.0, blue: 82/255.0, alpha: 1.0), //dark color
UIColor(red: 105/255.0, green: 94/255.0, blue: 133/255.0, alpha: 1.0), //purple color
UIColor(red: 85/255.0, green: 176/255.0, blue: 112/255.0, alpha: 1.0), //green color
]
func randomColor() -> UIColor {
var unsignedArrayCount = UInt32(colorsArray.count)
var unsignedRandomNumber = arc4random_uniform(unsignedArrayCount)
var randomNumber = Int(unsignedRandomNumber)
return colorsArray[randomNumber]
}
}
Steven Deutsch
21,046 PointsEverything looks fine here. Is everything else working properly in the app? It's making me crazy because I'd really like to solve this for you! I know it's not really the best solution but have you tried re-creating that IBAction?
Steven Deutsch
21,046 PointsSteven Deutsch
21,046 PointsHey Chris Wade,
Could you please post the code in your ColorWheel.swift file?
Thanks