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

Hi, how do I change the background colour of my label when the app reaches a certain score?

When my 'number' is less than 100 I want it to be a black screen, then a red screen until 500, a blue screen until 1000 etc... I have almost finished my first app, but cannot overcome this hurdle - please help! Do I put it into the ViewDidLoad() section (I am writing in swift on XCode)? How do I need to structure the code? An explanation would be much appreciated...

Jeff McDivitt
Jeff McDivitt
23,970 Points

Hi James -

Some more information would be helpful to assist you

  1. How are you counting myNumber? IS the user just entering the number into a text box?

Hey Jeff, every time the user taps the screen it increases by 1. I should also say that the button and label stretches across the entire screens. Really appreciate your willingness to help a beginner!

2 Answers

Jeff McDivitt
Jeff McDivitt
23,970 Points

Hi James - You could do it with an if statement, as far as where to write the code it would be in the button they they are tappings action

var myTaps = 0


if myTaps > 100 {

    // background color = black
} else if myTaps >=500 {

    // background color = blue
}

```Swift

Thanks! How do I actually specify which colour will appeal though? And, where snouts should I store that code? Under View Controller?

Jeff McDivitt
Jeff McDivitt
23,970 Points

Just start typing in background color in Xcode and then set it to whatever color that you want. Also yes if you left the viewController named that then it can go in there under the button action