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 trialGeoffrey Beatty
727 Points"Programming a Background" – stuck on code challenge
I can't for the life of me figure out what I'm doing wrong here. I'm on the last of three questions in the Crystal Ball lessons on "Programming a Background." It's giving me this error: "Remember that you need to add a subview and not insert a subview."
I've looked through the XCode documentation, tried different things. I'm truly stuck.
UIImage *image = [UIImage imageNamed:@"lolcat.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[catView.view addSubview:imageView];
14 Answers
Amit Bijlani
Treehouse Guest TeachercatView
is already an instance UIView
you don't need to access a view
property of it.
Your last line should be:
[catView addSubview:imageView];
Joseph Hall
10,865 PointsIt should look like this
[catView.view insertSubview:imageView atIndex:0];
Geoffrey Beatty
727 PointsSorry, but that didn't seem to work. I still get the same error.
Remember that you need to add a subview and not insert a subview.
Geoffrey Beatty
727 PointsThanks, Amit. That worked like a charm. I was getting stuck on the difference between insertSubview and addSubview. I didn't realize the instance negates the need to call the property.
Let me take this time to say that this lesson has been one of the best, most understandable lessons in coding that I've ever received. That counts both face-to-face and online courses. What's especially great is that I'm feeling like I can actually do this, rather than getting really discouraged reading some of the other tutorials.
From one teacher to another, you are doing a great job.
Amit Bijlani
Treehouse Guest TeacherThank you for the kind words Geoffrey! I really appreciate it.
Scott Lippman
2,498 PointsStuck here myself - you said that catView was already an instance of UIView - how do we know that?
Amit Bijlani
Treehouse Guest Teacher@Scott the very first line of the Code Challenge defines the catView
instance:
UIView *catView = [[UIView alloc] init];
ujz ujz
Courses Plus Student 6,664 PointsCould I replace the catView in [catView addSubview:imageView]; with self? So we'd have [self addSubview:imageView]; ?
When I tried it in the code challenge, it didn't work. I can't understand why
Lastly, unrelated to above but is there a way to search the forum?
Thanks!
Amit Bijlani
Treehouse Guest Teacher@yuji self
refers to the instance of a particular class. Hence, you see it inside the UIViewController
because it is referring to the instance of that particular view controller. In the case of the Code Challenge there is no view controller.
Kurt Archer
5,139 PointsHi Amit, Could you explain why this is NOT correct:
[self.catView addSubview:imageView];
I'm not fully getting the use of self. (pun not intended :p)
Amit Bijlani
Treehouse Guest Teacher@kurt self
refers to the instance of the class within which self
is defined. When you say self.catView
, you are saying access the property of myself. In the video a view is a property of the view controller hence self.view
. In the code challenge catView
is simply an instance variable and not a property of some class. The next stage called data modeling in the Blog reader explains this concept very nicely and it should be out next week.
Emma Garwood
297 PointsI also had this problem! I started with 'self'. Thanks for explaining... I'm not sure I get it fully, but will read on and hopefully that will compound the theory.
Thanks Amit!
Amit Bijlani
Treehouse Guest TeacherThis new stage in the "Build a Blog Reader app" explains self
in detail. http://teamtreehouse.com/library/ios-development/build-a-blog-reader-iphone-app/data-modeling
Timothy Chan
3,715 PointsGot stuck here as well. 4 hours of pulling my hair out, I decided to Google for help and landed on this page. I have no idea how I got here....or how to get back to here. Thanks for the answer.