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 trialom patel
Courses Plus Student 1,420 Pointsback ground color not changing
hi so i was making the simple app with swift in that we also change the background color when we press the button but in mine it is not changing when i add iad banner and i remove iad banner code then it changes so i want to know how can i keep the iad banner and also change the background color also my "show another fun fact" button background color changes with iad banner
om patel
Courses Plus Student 1,420 Pointshow
Michael Hulet
47,913 PointsJust copy all the code in the relevant file and paste it here
om patel
Courses Plus Student 1,420 Pointsimport UIKit
import iAd
class ViewController: UIViewController, ADBannerViewDelegate {
@IBOutlet var AdBannerView: ADBannerView?
@IBOutlet weak var funFactLable: UILabel!
@IBOutlet weak var funFactButton: UIButton!
let factBook = FactBook()
let colorWheel = ColorWheel()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
funFactLable.text = factBook.randomFact()
self.canDisplayBannerAds = true
self.AdBannerView?.delegate = self
self.AdBannerView?.hidden = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func showFunFact() {
var randomColor = colorWheel.randomColor()
view.backgroundColor = randomColor
funFactButton.tintColor = randomColor
funFactLable.text = factBook.randomFact()
}
func bannerViewWillLoadAd(banner: ADBannerView!) {
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
self.AdBannerView?.hidden = false
}
func bannerViewActionDidFinish(banner: ADBannerView!) {
}
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
return true
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
self.AdBannerView?.hidden = true
}
}
om patel
Courses Plus Student 1,420 Pointslet me know
om patel
Courses Plus Student 1,420 Pointsi deleted the ADBanner view outlet then pasted your code but it is still crashing or do i delete the self.canDisplayBanner Ad
Michael Hulet
47,913 PointsWhat is the crash log saying is the reason for the crash?
3 Answers
Michael Hulet
47,913 PointsYour problem likely stems from the fact that as soon as you set canDisplayBannerAds
to true
the iAd
framework takes your view
variable and assigns it to an ADBannerView
that it generates and manages itself. It places what's in your original view
variable in another variable called originalContentView
. Because of this, you also don't need to conform to ADBannerViewDelegate
, nor do you have to save a reference to your own ADBannerView
. After you delete the ADBannerView
from this view in Interface Builder, this should be a fixed version of your code:
import UIKit
import iAd
class ViewController: UIViewController{
@IBOutlet weak var funFactLable: UILabel!
@IBOutlet weak var funFactButton: UIButton!
let factBook = FactBook()
let colorWheel = ColorWheel()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
funFactLable.text = factBook.randomFact()
canDisplayBannerAds = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func showFunFact() {
var randomColor = colorWheel.randomColor()
//Your view is called originalContentView, since you're using canDisplayBannerAds
originalContentView.backgroundColor = randomColor
funFactButton.tintColor = randomColor
funFactLable.text = factBook.randomFact()
}
}
om patel
Courses Plus Student 1,420 Pointsyour coding gives me lots of treat and my application is crashing and i want to keep the iad banner and the background color please help me out if you can
thank you
Michael Hulet
47,913 PointsSetting canDisplayBannerAds
will create and show its own banner view. There's no need to create and manage one yourself. If you delete your ADBannerView
in your storyboard and use my code above (which you might wanna copy and paste again, as I might've made a few edits since you last did so), everything should work
Jas Singh
3,770 PointsThanks for this !! Not only have I solved the same problem but I understand why - to fix myself in the future (Y)
Marc Biggar
10,942 PointsIf you just keep your original code and add:
originalContentView.backgroundColor = randomColor
into your showFunFact method it should work also - did for me anyway
Michael Hulet
47,913 PointsMichael Hulet
47,913 PointsCould you please post all the code of your
class
here?