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

FunFact App problem. Cannot change view color

import UIKit import iAd

class ViewController: UIViewController, ADBannerViewDelegate{

var bannerView:ADBannerView?

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

let factBook = FactBook()
let colorWheel = ColorWheel()

override func viewDidLoad() {
    super.viewDidLoad()
    self.canDisplayBannerAds = true
    self.bannerView?.delegate = self
    self.bannerView?.hidden = true
    funFactLabel.text = factBook.randomFact()
    // 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.
}

@IBAction func showFunFact() {
    var randomColor = colorWheel.randomColor()
    funFactButton.tintColor = randomColor
    self.view.backgroundColor = randomColor
    funFactLabel.text = factBook.randomFact()
}

func bannerViewDidLoadAd(banner: ADBannerView!) {
    self.bannerView?.hidden = false
}

func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
return willLeave
}

func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
    self.bannerView?.hidden = true
}
}

Please format your code better and clearly explain the problem you are having before posting a question. This isn't exactly StackOverflow, but pretend it is.

As Rutger Farry said above me, it'd be great if you could add a little more context to this. Describe what you're doing, what you expect to happen, and what actually happens, and maybe post the code of your ColorWheel file? Also, pro tip: If you're using iAd banner ads, instead of going through all the trouble of managing banner ads, just import iAd at the top of your file, then in the viewDidLoad function of your view controller, set canDisplayBannerAds to true, like this:

import iAd
class ViewController: UIViewController{
    override func viewDidLoad(){
        super.viewDidLoad()
        canDisplayBannerAds = true
    }
}

Michael Hulet, it all works until I put in canDisplayBannerAds = true

I followed the code that Aaron Ackerman gave me right under here. I do not understand...

The App still works, but the view is not changing color

If setting canDisplayBannerAds to true is your problem, then you're not taking into account the fact that when you set that property, your view is then referred to as originalContentView. For example, here's your current showFunFact() function:

@IBAction func showFunFact() {
    var randomColor = colorWheel.randomColor()
    funFactButton.tintColor = randomColor
    //Notice you're referencing self.view, which, if you set canDisplayBannerAds to true, is the iAd banner
    self.view.backgroundColor = randomColor
    funFactLabel.text = factBook.randomFact()
}

Here's a fixed version of that function, if you set canDisplayBannerAds to true:

@IBAction func showFunFact() {
    var randomColor = colorWheel.randomColor()
    funFactButton.tintColor = randomColor
    //This will give you your expected result with canDisplayBannerAds enabled
    originalContentView.backgroundColor = randomColor
    funFactLabel.text = factBook.randomFact()
}

That will change your view's backgroundColor the way you want it, while still taking the easy way with iAd. Now you can delete all that extra ad handling code. Also note that I didn't reference self there. It's total preference, but I like going straight to the properties, instead of typing out self.

2 Answers

  1. Make sure your IBOutlet is connected.

  2. Compare your code with this.

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()

     funFactLabel.text = factBook.randomFact()


    }

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


    @IBAction func showFunFact() {

        funFactLabel.alpha = 0

        var randomColor = colorWheel.randomColor()
        view.backgroundColor = randomColor
        funFactButton.tintColor = randomColor
        funFactLabel.text = factBook.randomFact()


        UIView.animateWithDuration(2.0, animations: {
            self.funFactLabel.alpha = 1.0

        })

    }

}
  1. check your ColorWheel
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]

    }

}

I can get that to work, but I want to add the ad aswell :)

I still need help, please