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 trialBrennan Wallace
399 PointsAt what point is does segueLabelText get changed from "" to the text "Yay!..." text? Why do we need to declare it as ""?
Not quite sure about that transition. Also is the segueLabelText just passed in as a variable? If so I'm really not sure why we would have to declare it ahead of time.
2 Answers
Preston Skaggs
11,818 PointsIt's because the UILabel buttonPressed is a UI element that isn't available until the view loads. Therefore to set the text before the view loads, i.e. during a segue, you need to use a variable.
It is confusing and it seems that the Swift developers could have implemented a hoisting technique like JavaScript uses to use variables regardless of their type anywhere in code.
S B
4,497 PointsI'm hoping to get a bit more clarification on this topic. Here's how I see the step by step logic (please correct me if I'm wrong) -
- User clicks on 'aButton' in PlaylistMasterViewController.swift
- Since 'aButton' is linked (via a 'Push Segue') to PlaylistDetailViewController.swift, once it's pressed, the 'prepareForSegue' function is called.
- The function determines that the segue with the identifier showPlaylistDetail is true, and proceeds to prepare the transition to PlaylistDetailViewController.swift
- At this point, the UI Label 'buttonPressLabel' in PlaylistDetailViewController.swift has not been loaded as yet, so we need an empty String. This is where I'm a bit confused.
Can someone please clarify the detailed steps to complete the transition? I'd like to know the logic behind the empty String variable. I believe this answer will progress my understanding of this quite a bit.
Thank you!
james bunn
2,377 PointsI am also struggling with this topic. I understand the next sequence to be:
4.5 Set value "SegueLabelText" in the PlaylistMasterViewController to "Yay!, You pressed the button!"
- Transition to PlaylistDetailViewController
- Set value of "SegueLabelText" in the PlaylistDetailViewController to ""
- Set value of buttonPressLabel to ""
I'm assuming the variable "SegueLabelText" is already initialized in memory, before we "run" the app. Then, 4.5 overrides the empty string to be "Yay!, You pressed the button!". Next, buttonPressLabel references this text, not the empty string.
Any confirmation would be great!