Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

iAd + FunFact Problem! Can't change view color, please help

//
//  ViewController.swift
//  NyttigViden
I cannot change my view color. I can do if when I remove the Ad, but how do I make it work with the Ad? Please help.



//
//  Created by Andreas Poulsen on 16/01/15.
//  Copyright (c) 2015 Andreas Lyngholm. All rights reserved.
//


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
    }
}

6 Answers

Open Main.Storyboard Then Select the main View & then connect it to your ViewController by control + clicking & dragging it (Similar to how we connect Labels & Buttons)

@IBOutlet var superView: UIView!

And then where you want to change the color write the line like this:

superView.backgroundColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)

This will solve your problem.

Have you tried placing the banner funcs before the IBAction?

    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
    }
}
@IBAction func showFunFact() {
        var randomColor = colorWheel.randomColor()
        funFactButton.tintColor = randomColor
        self.view.backgroundColor = randomColor
        funFactLabel.text = factBook.randomFact()
    }

That I have tried. Still does not work. :(

I still need help, please

Anyway you can share this via github?

Jas Singh
Jas Singh
3,770 Points

I am having the exact same problem :( The facts change but not the View Colour