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

Deleted User

Can'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

As 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!

Andrea Sanchez
Andrea Sanchez
Courses Plus Student 2,132 Points

Good point, yes I actually did not consider using this as I was immersed in learning the current section, good tip, thanks!

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

Nope, 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
Andrea Sanchez
Courses Plus Student 2,132 Points

Thank 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
Amit Bijlani
Treehouse Guest Teacher

No catView is declared on the first line of the code challenge.

I got stuck on this one too. Thanks for the answer Amit!

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

@sam jacob that's because it's right answer.

Stefano Parziani
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

Deleted User

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
Chantel Martin
429 Points

Im stuck too. Pretty annoying. I enjoy moving on and earning badges. Lol. :)

Deleted User

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
Chantel Martin
429 Points

Thanks 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.

Deleted User

Ya I did :) I'm thinking it is a bug. It'll bug me if I move on without finishing it lol!!

Deleted User

Makes sense! Thank you! <3

Amit,

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
Dillon Carter
6,364 Points

I 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
PLUS
Ashley Kelley
Courses Plus Student 10,554 Points

Hey 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 :)

Would like to take a look at your block of code to see what's going on, Ash.

Ashley Kelley
PLUS
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!

Being 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
Stephan Soderberg
4,249 Points

Well 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
Sam Jacob
8,652 Points

When i copy paste the code [catView addSubview:imageView]; in the challenge it works...Very weird.

David Collins
David Collins
6,643 Points

I had this issue to, only because I didn't notice that catView was an instance variable rather than a property!!!

Wow spelled the method name as 'addSubView' instead of 'addSubview' ..... been on here for too long!