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 Passing Information Between Views

S B
S B
4,497 Points

Do I still have to to pass data from the main view controller to the destination view controller in this case?

Let's say that we have some additional data in MusicLibrary that will only be used in the destination view controller (not in the main one). Do I still have to pass this data to the destination view controller using prepareForSegue? If not (i.e. if I access the playlist data from the destination view controller), how would I know which playlist was accessed from the first view controller in order to display the correct data on the destination view controller UI?

2 Answers

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

Actually, none of the data from the detail view controller goes in the master. The answer to your second question is in the last stage of the course, Refactoring your code.

First of all, you should do well to know that in the MusicLibrary's library database, all data is categorized by which playlist it falls under. This means that because you are passing a Playlist through prepareForSegue, you are simultaneously passing whatever data there is about that playlist.

For your second question; do you remember writing the Playlist struct's initializer? It asks for one value – index: Int – which is used to grab the data at that index in MusicLibrary. In the master view controller, the six buttons for each playlist are put in the array playlistArray.

It's important to note that the IBOutlets placed in that array are in the SAME ORDER as in MusicLibrary. Also note that the items in the array refer to the same objects as the original IBOutlets because they are reference types. This allows the button's index in the array to be used in the initialization of the playlist being sent to the destination view controller.