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

iOS

Ljubica Kovacevic
Ljubica Kovacevic
2,737 Points

Code challenge 2 of 2 - changing the view's color

I passed part 1 with this color, and I've assigned it to the view's background. However it tells me: Bummer! Make sure you assign an instance of UIColor representing blue color as the background color.

class ViewController: UIViewController {

let blueColor = UIColor.blue
//let blueColor = UIColor.blueColor() I saw this somewhere, but it doesnt work

  override func viewDidLoad() {
      super.viewDidLoad()
      // Do any additional setup after loading the view, typically from a nib.
  }

  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
      view.backgroundColor = blueColor
  }

 //let blueColor: UIColor = UIColor(red: 0/255.0, green: 0/255.0, blue: 255, alpha: 1.0)
 //let blueColor = UIColor(red: 0/255.0, green: 0/255.0, blue: 255/255.0, alpha: 1.0) for some reason this did not work
}

I believe you need to put "view.backgroundColor = blueColor" on the viewDidLoad() method as opposed to the didReceiveMemoryWarning() method.

3 Answers

Gavin Hobbs
Gavin Hobbs
5,205 Points

Hey Ljubica,

I went back and tested it out and Arsen Holsteinson was right. You need to move the view.backgroundColor = blueColor to the viewDidLoad() function rather than the didReceiveMemoryWarning() function. Here's the correct code...

class ViewController: UIViewController {
    // Set the blueColor to blue
    let blueColor = UIColor.blue

   // Or you could do this...
  // let blueColor = UIColor(0, 0, 255, 1.0)

  override func viewDidLoad() {
      super.viewDidLoad()
      // Do any additional setup after loading the view, typically from a nib.

       // Set the background color here
       view.backgroundColor = blueColor
  }

  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.

     // Don't do anything here
  }
}

You can't set the background color before the viewDidLoad() because, well... the view hasn't loaded yet.

Additionally, the only time the didReceiveMemoryWarning() function is set off is when the app receives a memory warning so that wouldn't be a good place to put the background-color-set statement either.

Therefore, the best place would be in the viewDidLoad() because the statement is only executed once the view is loaded and ready for instructions.

Think of it as an if statement... if the view is loaded--do this. if the app receives a memory warning--do this do this being whatever you put inside those functions.

Hope this helps!

-Gavin

Jaroslaw Adamowicz
Jaroslaw Adamowicz
11,634 Points

Actually the same challange is questioned here

But just for your convinience I will put it here below as well :)

Hi,

in this challenge you have class with two methods inside.

Your first task is to add new property. Class property can be variable or constant named blueColor, so either:

let blueColor = UIColor.blue

or

var blueColor = UIColor.blue

You can always use more advanced way of course using UIColor initializer directly, like this for example:

var blueColor = UIColor(red:0, green:0, blue:255, alpha:1.0)

Just keep in mind, one of this lines should be added somewhere inside of the ViewController class, but not inside of the methods!

Your second task is to use this property to change background of the view inside ViewController. Remember, view is literally a property of ViewController!

Also you one to do this in method called viewDidLoad. This method should be self-explanatory, IOS calls this method when your view is ready and load, so you can now customize it (like change its background) :D

So it will look like this:

view.backgroundColor = blueColor

That's all to it!

Let me know if you still need some help.

Cheers!

BR, Jarek

Ljubica Kovacevic
Ljubica Kovacevic
2,737 Points

OMG thank you, Arsen, I didn't realize that. What a dumb mistake.