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 trialCan't seem to get the "Programming a Background Image" problem...
Hey everyone :)
So I've been stuck on this problem for a while. Seems easy enough but I can't seem to think straight today, lol!
So here's the problem:
Add the UIImageView as a subview to the instance of UIView named 'catView':
The answer I have is:
UIView *catView = [[UIView alloc] init];
UIImage *image = [UIImage imageNamed:@"lolcat.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
It keeps saying that
[self.view addSubview:imageView];
is incorrect, but not sure why.
Any help would be appreciated! Thanks :)
22 Answers
Stephen Whitfield
16,771 PointsAs you guys move on through the rest of the iOS courses, stuff like this will become second nature. Amit points out several times that referring back to the documentation is important, but in these first few lessons I rarely did. In the "Create a Blog Reader" lessons, I find myself constantly referring back to the documentation with the magical 'hold option + click' shortcut and it is altogether helping me learn iOS programming much easier than before. Make it a habit now so when you get to other parts of iOS programming, you won't be discouraged!
Amit Bijlani
Treehouse Guest TeacherNope, it's not a bug. You would use the self
notation only if catView
was a property but it isn't. It is simply an instance variable hence the code should be:
[catView addSubview:imageView];
Andrea Sanchez
Courses Plus Student 2,132 PointsThank you Amit! I have been staring at my code for a while now :(
UIView *catView = [[UIView alloc] init];
UIImage *image = [UIImage imageNamed:@"lolcat.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
so catView is an instance variable of parent class UIView?
Amit Bijlani
Treehouse Guest TeacherNo catView
is declared on the first line of the code challenge.
Max Jordan
1,289 PointsI got stuck on this one too. Thanks for the answer Amit!
Amit Bijlani
Treehouse Guest Teacher@sam jacob that's because it's right answer.
Stefano Parziani
2,056 Points[self.view addSubview:imageView];
you should refer to the name of your UIView instance.... if you read the first line of your code you will see that you instance UIView with the name catView.... so:
[self.catView addSubview:imageView];
should be correct
Thanks for the reply :) Unfortunately, I tried it and it's still considered to be incorrect. Not too sure what to do, lol!
Chantel Martin
429 PointsIm stuck too. Pretty annoying. I enjoy moving on and earning badges. Lol. :)
Same here :) I read somewhere that the system can be picky when checking your code, so I'll just keep on trying and let ya know if I find it!
Chantel Martin
429 PointsThanks much. I was wondering if there was a bug. Did you realize that a new line was automatically created when moving to Task 2 from 1 but not when moving on to Task 3 from 2? Maybe Im thinking too much? Eh. I just want to get the challenge correct.
Ya I did :) I'm thinking it is a bug. It'll bug me if I move on without finishing it lol!!
Makes sense! Thank you! <3
Stephen Whitfield
16,771 PointsAmit,
Thanks. This was driving me crazy. Objective C seems so alien to me. Can't understand why. I'm great at many of the trending codes used today (Python being my favorite), but I can't seem to 'get' Objective C too well.
Dillon Carter
6,364 PointsI also got stuck on this last part of the challenge! I used multiple variations of the final code line to complete and understand the challenge but they all come back as incorrect...
Even what Amit said to use: [catView addSubview:imageView];
Edit: I used the exact same code which wouldn't work but when I copy and pasted the same code from this thread it worked........ Weird lol
Ashley Kelley
Courses Plus Student 10,554 PointsHey guys,
I too have worked through the tasks fairly well but I am becoming increasingly frustrated with this specific task, mainly the final line of code as Amit has provided us with above. I am still experiencing the same error message even though I have tried many variations and even pasting the line of code in.
Any ideas? Thanks :)
Amit Bijlani
Treehouse Guest Teacher@Ashley Can you post your code?
Stephen Whitfield
16,771 PointsWould like to take a look at your block of code to see what's going on, Ash.
Ashley Kelley
Courses Plus Student 10,554 Points@Amit, @Stephen
I managed to resolve the problem. I started the coding challenge from the start, re-typing each line of code and simply pasting in the line of code provided by Amit (which I had done previously) and the check facility accepted it.
It appears to be a slight bug whereby if you get the code wrong and try again with the 3rd line the check facility won't accept it. So you have to come out of the task and start fresh.
Thanks for replying!
Rodney Leggero
1,019 PointsBeing a Newby I normally get stumped on each code challenge but have always worked thru it. On this particular one I have re watched the video 12 times, read the transcript and even wrote the spoken words as they happened, pausing to not miss a single detail. I never would have figured it out if it were not for this discussion. I only hate that I spent hours and hours on it.
Stephan Soderberg
4,249 PointsWell we're not alone ;) I also got stucked to the frustrating point of NOT having fun any more.
I guess it's a little bit to early in the course with this poser question ;) However I think Amit is a great teacher.
Sam Jacob
8,652 PointsWhen i copy paste the code [catView addSubview:imageView]; in the challenge it works...Very weird.
David Collins
6,643 PointsI had this issue to, only because I didn't notice that catView was an instance variable rather than a property!!!
Kyle Jablonski
10,159 PointsWow spelled the method name as 'addSubView' instead of 'addSubview' ..... been on here for too long!
Andrea Sanchez
Courses Plus Student 2,132 PointsAndrea Sanchez
Courses Plus Student 2,132 PointsGood point, yes I actually did not consider using this as I was immersed in learning the current section, good tip, thanks!