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 trialzhengyangdong
2,623 PointsI 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!"
}
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)?
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)
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?
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
14,575 PointsHello :
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.
zhengyangdong
2,623 Pointszhengyangdong
2,623 PointsThank you so much! This is making it much better!
I still have a few questions:
Thanks a lot!
Jhoan Arango
14,575 PointsJhoan Arango
14,575 PointsHello 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.