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

Cordell Key
Cordell Key
1,130 Points

Programming a Background Image (Help!)

Hi - Im keep getting the following ERROR message "Missing '[' at start of message send expression" where I'm creating an instance of UIImage View. Please see my code example below:

UIImage * image[UIImage imageNamed:@ "background.png"]; UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; [self.view addSubview:imageView];

Any thoughts on what might be missing or incorrect?

Thanks

3 Answers

Peter Pult
Peter Pult
8,095 Points

Looks like you missed a = sign in your first line.

Change

UIImage * image[UIImage imageNamed:@ "background.png"];

To

UIImage * image = [UIImage imageNamed:@ "background.png"];

Good catch peter. For the purpose of this tutorial, I believe by using 'addSubview' the image would be laid on top of the other UI elements. Looks like there were a few bugs. Again, good catch.

Cordell Key
Cordell Key
1,130 Points

Peter Pult - Thank you so much for catching this error.....its always the most obvious things I seem to overlook!

Peter Pult
Peter Pult
8,095 Points

Kyle Jablonski your method of using insertSubview is probably the safest option but if it's the first thing you add you end with the same result. Cordell Key it's always the small things...:)

UIImage *image = [UIImage imageNamed:@"background.png"];

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

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

You may want to add @synthesize imageView; to the top of your ViewController.m file and add this UIImageView as a property in the ViewController.h @property(strong, nonatomic) UIImageView *imageView; So that you can then use the following to make the background the image

UIImage *image = [UIImage imageNamed:@"background.png"];

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

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

Looks like your problem was the call to addSubview you need to insertSubview at the base index.

Cordell Key
Cordell Key
1,130 Points

Kyle Jablonski - Thanks for replying to my message! I just took a quick look at your response and it looks like I'll need to make updates to the [self.view addSubview:imageView];, where the updated code would be [self.view insertSubview:imageView atIndex:0];

However I'm a little lost on (A) adding @synthesize image view to the View Controller.m and (B) Adding UIImage View as property in the View Controller.h @property (strong, nonatomic) UIImageView *imageView;

Sorry, I'm still a novice at developing in IOS......any help you can provide will be most appreciated.

Thank you

Yeah, after posting that i realized i would probably have confused you a little. But using @synthesize will save you time in the long run. Take a look at it in the documentation when you have a chance. But for your purposes you can ignore that comment, since its not your error.

Cordell Key
Cordell Key
1,130 Points

UIImage * image[UIImage imageNamed:@ "background.png"];