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
Claire Emery
1,588 PointsCGRect and Frame video contradicts code challenge?
Hi, I'm doing 'build a simple iphone app for iOS7' > 'understanding views and controllers'
The video 'CGRect and Frame' uses the following expression:
self.labelName.frame = CGRectMake(x,y,w,h);
and demonstrates that putting this in viewDidLoad does not work - it doesn't explain why, but much of the tutorial is spent demonstrating it. Instead you have to add viewDidAppear and set the frame properties there.
The subsequent code challenge asks you to do the exact same thing, specify frame properties for a label. But it only accepts the code as a right answer if you put it in viewDidLoad. Which contradicts the video.
I was already confused and am now totally baffled. Is it something that I'm not understanding, or do the two really contradict each other?
2 Answers
Amit Bijlani
Treehouse Guest TeacherA view controller loads all the views and controls from the storyboard, the viewDidLoad method signals that this loading is complete so you can execute your custom code. However, there are times when the loading of the view and the execution of the viewDidLoad might happen simultaneously. If you put frame modification same code within the viewDidLoad method you will notice that the label's frame remains unchanged. The viewDidAppear method gets executed after viewDidLoad which ensures that your frame will get modified.
The main goal of the code challenge is to test your comprehension ability. If it is exactly like the tutorial then you would simply be copying and pasting code without understanding what you were actually doing.
Trevor Gerzen
2,749 PointsThat totally makes sense Amit, but in the video you say that it may not be the best way to go about modifying the label. So in my thinking I was relating the code challenge to the context of the tutorial, thinking that the best way to go about completing the task at hand would be to do the frame modification within the viewDidAppear method. I understood after it told me that it was wrong that it was ok to complete the code challenge by doing the frame modification within the viewDidLoad method, but I didn't learn why. It was more a trial-and-error response from me. Hope that makes some sense.