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

Add UIImageView as a subview to the instance of UIView named catView

What is wrong with the last line? I don't understand the concept also...

UIView *catView = [[UIView alloc] init];

/* Write your code below this line */

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

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

[self.view addSubview: catView];

8 Answers

Trevor Gerzen
Trevor Gerzen
2,749 Points

Naga's response helped me understand that challenge as well. I didn't realize I could write:

[catView addSubview:imageView];

I kept trying to write:

[catView.view …];

Thanks!

I had trouble with this challenge.... This is what I came up with:

[catView addSubview:imageView];

Didn't make sense seeing that we were just told to do this. [self.view insertSubview:imageView atIndex:0];

And this. [self.view addSubview:imageView];

So getting to this, isn't the first logical answer based on what we were just taught in the video. [catView addSubview:imageView];

I agree that it's not directly logical. However, thinking about what we're being asked to complete and then relating it to the limited scope of the code were working with it makes sense to use 'catView' instead of 'self.view' because we can't assume anything else about the code outside of what we're given.

Also, it says "Add the UIImageView as a subview to the instance of UIView named 'catView'." Armed with information you can look at the documentation to see the difference between the activity request of insertSubview and the challenge of addSubview.

Thinking retrospectively, that's how you can logically arrive at the connection between what we're taught and expanding it to encompass a slightly alternate path.

Plus, you can use the forum to find the answer and then work backwards to deduce it as well!

*I struggled with it too.

Well..You are trying to add catView to your superview(self.view). But catView has nothing inside it, you didn't even specified its frame property.

Possible Solution:

UIView *catView =[[UIView alloc] initWithFrame:CGRectMake(CGFloat x , CGFloat y, width, height)];

/* Write your code below this line */

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

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

/* Optional: Define frame property for imageView*/

imageView.frame=CGRectMake(0 , 0, imageView.frame.width, imageView.frame.size.height);

/* Code if you want to add imageView to catView*/

[catView addSubView: imageView];

[self.view addSubview: catView];

Hope it helps :)

@Grover - are you working on a code challenge?

If you're encountering troubles would you be able to post a link to the code challenge, and a link to working code on the forum?

Also, you can email me at help@teamtreehouse.com for help ^.^ please send me a screenshot of the code challenge when you experience that error. Thanks!

Sean Perryman
Sean Perryman
13,810 Points

Also had trouble with this code challenge, finding this page helped. Was trying to call self.view and/or catView.self.

very good help thanks~!!!!