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

I don't quite get what the override func prepareForSegue is for...

I don't understand why what this function is doing and how it helps us to modify the property of label in another view controller.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "showPlaylistDetail" {
            let playlistDetailController = segue.destinationViewController as! PlaylistDetailViewController
            playlistDetailController.segueLabelText = "Yay!"
        }
  1. Here in this function, the method "segue.identifier" is referring to which segue? Or does it refer to all the segues in the project and then look for the identifier that matches with the one specified (showPlaylistDetail)?

  2. In let playlistDetailController = segue.destinationViewController as! PlaylistDetailViewController, what is the type of playlistDetailController? In this line, does the "segue" in "segue.destinationViewController" refer to the segue with identifier "showPlaylistDetail"? If so, why are we specifying the destination view controller? (since the segue here is the segue called "showPlaylistDetail", which we already declared its destination in main.storyboard)

  3. Why are we modifying the label text from Master view? Isn't it much easier if we do it directly in detailed view controller? And how does this video demonstrates how the information between view controllers is passed?

  4. Why should we create an empty string for buttonPressLabel called segueLabelText in our DetailViewController in order for MasterViewController to refer to while changing the text? The video mentions that this prevents the app from crushing but I don't quiet get why. Since the playlistDetailController is referring to everything in the DetailViewController, wouldn't everything be loaded by then?

I am so confused...

1 Answer

Jhoan Arango
Jhoan Arango
14,575 Points

Hello :

All of these are valid questions.. I will do my best to try to help you understand segues.

A segue method is used to pass "information" or "data" from one viewController to another. Let's say you are creating an app, where you have two viewControllers. Each viewController "controls" a view, hence the name "viewController". At some point you may want to pass information between them. This can be achieved with a segue.

Your first question:

segue.identifier basically points to the segue that you created with that specific identifier, so you may have different segues in your project, and that's why they need identifiers, so that the system knows, which one you are referring to.

Your second question:

let playlistDetailController = segue.destinationViewController as! PlaylistDetailViewController

destinationViewController is a store property of type "UIViewController". Notice how we are accessing it with the dot. segue.destinationViewController. To this store property we are casting PlaylistDetailViewController, which is the viewController that we want to send information to. Once is casted, then we can access it's properties so that we can assign them values. In other words, is like creating an instance of PlaylistDetailViewController, but this process is called down casting.

Now you can access its properties.

playlistDetailController.properties 

Your third question:

Yes, it is easier to do it as you mentioned, but he is doing it this way to demonstrate how it's done through a segue.

Your fourth question:

What he is doing there is creating a store property that has an empty string. This property, with the empty string is what is going to receive the new value from the first viewController (Yay, you pressed the button), then he uses the method viewDidLoad(), to assign (Yay, you pressed the button) into buttonPressedLabel.text once the view has load.

If you still have any more doubts, please let me know. I will try to go further in my explanation.

Thank you so much! This is making it much better!

I still have a few questions:

  1. What is down casting exactly? Why can't we assign "let playlistDetailController = PlaylistDetailViewController"?
  2. I noticed that in the video the teacher is using: "let playlistDetailController = segue.destinationViewController as PlaylistDetailViewController" without an exclamation mark after "as". I'm working with Xcode 7.2 and Swift 2.0 so the compiler suggested me to change "as" to "as!" with an exclamation mark. It works and the results are the same as in the video. What is this "!" for? Is it a new feature introduced in Swift 2.0? It kind of looks like for force unwrapping "!" mark that I've seen before...

Thanks a lot!

Jhoan Arango
Jhoan Arango
14,575 Points

Hello again:

By doing "let playlistDetailController = PlaylistDetailViewController()" you will be creating a new instance of PlaylistDetailViewController, and we do not want that. That's why we are down casting it.

For a better down casting explanation, you can click here.

The bang operator "!" in this case is used to force downcast. When the video was created, they used "as" instead of "as!" since they were doing it with an older version of swift.