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 Build a Playlist Browser with Swift Using a Navigation Controller With Segues Modifying the UI

Brennan Wallace
Brennan Wallace
399 Points

At 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

It'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
S B
4,497 Points

I'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) -

  1. User clicks on 'aButton' in PlaylistMasterViewController.swift
  2. Since 'aButton' is linked (via a 'Push Segue') to PlaylistDetailViewController.swift, once it's pressed, the 'prepareForSegue' function is called.
  3. The function determines that the segue with the identifier showPlaylistDetail is true, and proceeds to prepare the transition to PlaylistDetailViewController.swift
  4. 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
james bunn
2,377 Points

I 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!"

  1. Transition to PlaylistDetailViewController
  2. Set value of "SegueLabelText" in the PlaylistDetailViewController to ""
  3. 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!