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

Crystal ball code challenge background image iOS

Hey guys, So I have been stuck on the code challenge for Programming a Background Image for the Crystal Ball iPhone App. I have been trying to get this right and there are already two discussions on this topic but they don't seem to answer the question. Here's what is required: 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 what I have so far but I don't know how to proceed. It keeps on giving me an error saying “use of undeclared identifier self” but it works in xcode.

UIImage *backgroundImage = [UIImage imageNamed:@"background"];

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

[self.view insertSubview:imageView atIndex:0];

So, let me know if I’m missing something. Thanks.

3 Answers

Here's the signature for UIImageView's initWithImage:

- (id)initWithImage:(UIImage *)image

As you can see, the argument it takes is an UIImage object, and what you have in your code (@"background") is only an NSString. Replace that with the UIImage you declared earlier and you'll be all set.

thanks it worked

Here's the complete solution: UIImage *backgroundImage = [UIImage imageNamed:@"background"];

UIImageView *imageView = [[UIImageView alloc] initWithImage:backgroundImage];

Why doesn't the UIImage object require an 'alloc'?