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.


Pablo Kvitca
Pablo Kvitca
Courses Plus Student 9,624 Points

Code Challenge: Programming a Background Image (iOS 7)

I'm stuck at this code challenge:

"Our Random Quotes app is looking bland and we need to add a background image to it. We started to create a UIImageView, however, the code does not seem to work. Figure out what is wrong and if you need to declare another object to complete this declaration."

This is my current input:

// The following code is excerpted from 
// the viewDidLoad method of THViewController.m

// Fix the code below 
// Hint: First create an instance of UIImage

UIImage *backgroundImage = [UIImage imageNamed:@"background.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:backgroundImage];
[self.view insertSubview:imageView atIndex:0];

The message says: "Bummer! Remember that UIImageView needs to be initialized with a UIImage object."

Also, I get the following error (when I click "Preview"): "use of undeclared identifier 'self' [self.view insertSubview:imageView atIndex:0]; ^ 1 error generated."

Can anyone tell me what's wrong and how to fix it?? Thank you!

5 Answers

The first thing it is asking you to do is fix is the code that's wrong.

This is what is the code they give you: UIImageView *imageView = [[UIImageView alloc] initWithImage:@"background"];

This is what it should be: UIImageView *imageView = [[UIImageView alloc] initWithImage:backgroundImage];

The second part is to add a line of code ABOVE: UIImage *backgroundImage = [UIImage imageNamed:@"background"];

This is what it will look like when you're done: UIImage *backgroundImage = [UIImage imageNamed:@"background"]; UIImageView *imageView = [[UIImageView alloc] initWithImage:backgroundImage];

Bogdan is right, you don't need the [self.view insertSubview:imageView atIndex:0]; for this challenge.

Bogdan Raduta
Bogdan Raduta
1,076 Points

Remove [self.view insertSubview:imageView atIndex:0]; because probably the compiler doesn't expect this line in that particular challenge.

Miguel Martin
Miguel Martin
21,703 Points

just do this:

UIImage *backgroundImage = [UIImage imageNamed:@"background"]; UIImageView *imageView = [[UIImageView alloc] initWithImage:backgroundImage];

Stone Preston
Stone Preston
42,016 Points

hmm try

[self insertSubview:imageView atIndex:0]

pretty sure its view controller method, not a view.

Wow, this was so infuriating. The instructor gave us the wrong information, I couldn't figure this out for like a week and was so discouraged. Looking at my notes, then finally pausing the video in another window, typing in the code exactly as it was presented. Only to be told I was wrong some more. I wouldn't expect that when paying 50$ a month, I would have to deal with something that is EASILY fixed with a little bit of attention to detail. 12.50$ a week and the frustration of being so discouraged that I am too dumb to learn how to do this. Bittersweet, finding out that all of this is because the instructor doesn't give a shit.

Renewing my membership next month? Doubtful.