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 Build a Simple iPhone App with Swift 2.0 Improving Our User Interface Random Colors

Emmanuel Darmon
Emmanuel Darmon
6,115 Points

"Show Another Fun Fact" color is not changing

Background color change but not the text. How comes?

@IBAction func showFunFact() {
        let randomColor = ColorModel().getRandomColor()
        view.backgroundColor = randomColor
        funFactButton.tintColor = randomColor
        funFactLabel.text = factModel.getRandomFact()
}

7 Answers

Justin Beaudry
Justin Beaudry
2,775 Points

For me the problem was that I had set a 'Text Color' in the Attribute Inspector. I set the 'Text Color' to default and matched the Tint to the color value of the initial background color.

Emmanuel Darmon
Emmanuel Darmon
6,115 Points

"and matched the Tint to the color value of the initial background color" What do you mean here ?

Justin Beaudry
Justin Beaudry
2,775 Points

I meant that I set the Tint on the button to the same color that I had set the View Background to in the Attributes Inspector.

Justin Beaudry
Justin Beaudry
2,775 Points

But it seems after thoroughly reading the thread here that someone else has answered this question with nearly the same answer.

Rachel Hawkins
Rachel Hawkins
940 Points

In the storyboard, select your button and in the attributes section, your text colour should be on default? And then your tint colour should match your initial background colour?

Jhoan Arango
Jhoan Arango
14,575 Points

Hello :

@IBOutlet weak var funFactButton: UILabel!

shouldn't this be ?

@IBOutlet weak var funFactButton: UIButton! // Button

it's currently a label, and it should be a button.

Emmanuel Darmon
Emmanuel Darmon
6,115 Points

You right! But it didn't fix my problem... :(

Jhoan Arango
Jhoan Arango
14,575 Points

When doing the change, did you make sure to disconnect everything properly ? And then do the connection again ?

Jhoan Arango
Jhoan Arango
14,575 Points

I also just noticed another small bug..

  let colors = [
        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), // ERASE THIS COMMA //green color

       // Erase the last comma
    ]

Try that..

Emmanuel Darmon
Emmanuel Darmon
6,115 Points

Thanks again! Hope this would fix, but not yet... :(

Emmanuel Darmon
Emmanuel Darmon
6,115 Points

And yes, when I fix a bug, then I click on the black square to stop the application and then I click on the play button again.

Jhoan Arango
Jhoan Arango
14,575 Points

Ummm.. I would try to help you further, but I am going to work, how about downloading his code from the project downloads, and changing yours to his.. or compare them side by side. Perhaps there has to be a wrong connection of buttons.

Also try to go to "product" then hit clean, and then build again. It's on the top menu of your Xcode.

Emmanuel Darmon
Emmanuel Darmon
6,115 Points

Ok thanks a lot Jhoan Arango for your help! I'll let you know of course if I can figure out where is the bug.

Martin Wildfeuer
PLUS
Martin Wildfeuer
Courses Plus Student 11,071 Points

Ok, let's see if we can debug this remotely. I know this is not an answer but I don't know where else to put his :)

First of all set a breakpoint at the following line:

override func viewDidLoad() {
    super.viewDidLoad()
    funFactLabel.text = factModel.getRandomFact() // Set a breakpoint here
}

To set a breakpoint, click on the line number, a blue arrow should appear. Xcode will stop execution here and lets you debug variables and states. To toggle a breakpoint, click on it and it will grey out. To remove a breakpoint, drag it to the left.

Run your app.

When execution halts, make sure you have the debug area at the bottom expanded. You should see two areas: On the left, the current environment variables and objects, on the right the console..

Let's try two things, type into the console on the right:

  • po funFactButton
  • po factModel.getRandomFact()

Please post the console output here so we can debug further.

Emmanuel Darmon
Emmanuel Darmon
6,115 Points
(lldb) po funFactButton
<UILabel: 0x7f951be794b0; frame = (40 93; 118 22); text = 'Did you know?'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7f951be796b0>>

(lldb) po factModel.getRandomFact()
"Ants stretch when they wake up in the morning."
Martin Wildfeuer
Martin Wildfeuer
Courses Plus Student 11,071 Points

Oh, excuse me, the first one should be po funFactLabel, please try that one, too :)

Emmanuel Darmon
Emmanuel Darmon
6,115 Points
(lldb) po funFactLabel
<UILabel: 0x7f951be77690; frame = (40 268; 520 64.5); text = 'Mammoths still walked the...'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7f951be77c70>>
Martin Wildfeuer
Martin Wildfeuer
Courses Plus Student 11,071 Points

Ok, thanks! So, funFactButton is a UILabel instead of a UIButton, that's not the case in the project files from Treehouse. Clicking on the label should basically do nothing. So from what I can see here, my guess is:

When your launch the app the text is set for one time, as well as the color, but clicking on Did you know, does nothing. Is that correct?

Jhoan Arango
Jhoan Arango
14,575 Points

We tried fixing that part, perhaps he just changed the name from UILabel to UIButton without disconnecting and reconnecting by control dragging.

I think that's where the problem is.. A miss connection when he changed it.

Martin Wildfeuer
Martin Wildfeuer
Courses Plus Student 11,071 Points

Hi Jhoan Arango! Yes, I saw that you already found that bug, I just wanted to make sure that it was rewired correctly in storyboard, but as you said, that's where the problem is.

It seems that, instead of having wired up funFactButton with the actual button, it was wired up with the Did you know? label accidentally. Rewiring stuff in Storyboard is a bit tricky and hard to explain remotely.

My only idea right now would be deleting the Did you know? label and the button in Storyboard first. Then add them again in Storyboard and redo the connections between view controller and button, that is reference and action.

Similar problem here. When the text color of the funFactButton is set to "default" in the attributes inspector, the simulator show the default blue to start. Then the random color text that matches the background colors as written in the code works. But as soon as I change the text color for the button from default to my view's background color in the Main storyboard, then the text color won't change from that initial color when I am in the simulator.

I have re-established my connections and checked that they are correct. Otherwise everything works just fine.

Jhoan Arango
Jhoan Arango
14,575 Points

Hello :

This method looks like it's Ok, but maybe the problem is somewhere else, and you are only showing part of the code. Maybe posting more of it may help, like the FactModel struct.

Emmanuel Darmon
Emmanuel Darmon
6,115 Points
ColorModel.swift
import UIKit
import GameKit

struct ColorModel{
    let colors = [
        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 getRandomColor() -> UIColor {
        let randomNumber = GKRandomSource.sharedRandom().nextIntWithUpperBound(colors.count)
        return colors[randomNumber]
    }
}
FactModel.swift
import GameKit

struct FactModel{
    let facts = [
    "Ants stretch when they wake up in the morning.",
    "Ostriches can run faster than horses.",
    "Olympic gold medals are actually made mostly of silver.",
    "You are born with 300 bones; by the time you are an adult you will have 206.",
    "It takes about 8 minutes for light from the Sun to reach Earth.",
    "Some bamboo plants can grow almost a meter in just one day.",
    "The state of Florida is bigger than England.",
    "Some penguins can leap 2-3 meters out of the water.",
    "On average, it takes 66 days to form a new habit.",
    "Mammoths still walked the Earth when the Great Pyramid was being built."]

    func getRandomFact() -> String {
        let randomNumber = GKRandomSource.sharedRandom().nextIntWithUpperBound(facts.count)
        return facts[randomNumber]
    }
}
ViewController.swift
import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var funFactLabel: UILabel!
    @IBOutlet weak var funFactButton: UILabel!

    let factModel = FactModel()

    override func viewDidLoad() {
        super.viewDidLoad()
        funFactLabel.text = factModel.getRandomFact()
    }

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

    @IBAction func showFunFact() {
        let randomColor = ColorModel().getRandomColor()
        view.backgroundColor = randomColor
        funFactButton.tintColor = randomColor
        funFactLabel.text = factModel.getRandomFact()
    }
}