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 trialJames Smith
Courses Plus Student 637 PointsHi, 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...
James Smith
Courses Plus Student 637 PointsHey 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
23,970 PointsHi 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
James Smith
Courses Plus Student 637 PointsThanks! How do I actually specify which colour will appeal though? And, where snouts should I store that code? Under View Controller?
Jeff McDivitt
23,970 PointsJust 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
Jeff McDivitt
23,970 PointsJeff McDivitt
23,970 PointsHi James -
Some more information would be helpful to assist you