prepare(for segue: UIStoryboardSegue, sender: Any?)
…… and this is why we are really here.
Segues I promise, but let me tell you a little about a Lambda build week. It started on a Monday, and it was SCARY! I felt like I was going into a race that I had already lost. It was overwhelming and really quite soul crushing, simply because I was the only iOS dev on my project. I approached my TL after the first day because I had decided to flex *swallows pride* and repeat the first unit. However, I did work all the way through build week, and honestly by the end I had learned and understood so much more than I realized. I actually wished I would have hung in there and continued on, but now I am very confident in the skills I have learned thus far.
I want to share with you ONE of the two major breakthroughs I had during my first build week. Before I get into that though here are some general thoughts I would like to share as a newcomer to iOS development.
You know more than you think you do. If you are unsure about it, then create a simple app to solidify your curiosity.
Trust yourself. Swift is like words to a language. You compose the sentence you want to say. So write the code you want to write, and just use the tools you’ve been provided.
Okay, now on with the breakthrough. So, throughout my first three weeks at lambda I learned about a segue, but I NEVER KNEW what it did until build week. I remember sitting at my computer thinking to myself “how do I get the information from the cell in this TableViewController, into my DetailViewController???” Well, let me tell you the force is STRONG with the segue.
Note that in both my TableView and DetailView I have two grailController variables, one is a new instance of GrailController and the other would be an a GrailController object (if my language is off apologies). I promise I’ll make up for it.
Now, we can clearly see that we want the information from our tableViewCell on the left to populate the same information on the right! AND this is where I learned that not only does the segue take you from one view to the next, but it can carry DATA with it!
If you look at line 43 and 44 in my segue you may see me setting something to itself?? In a way that is exactly what is happening. I created an instance in my initial view, and set all of my methods to reflect that instance, now I need my detail view to reflect that instance as well.
For line 43, I am allowing my detail view to have access to the properties and methods in my grail controller class by setting the grailContoller object in my detailViewController to the same grailController in my tableViewController.
For line 44, this is where the magic happens! Take note, we do NOT simply need a grail object on the next view, we need a SPECIFIC grail object (the one the user selected). Reread this, because I know it sounds familiar!! In order to get a specific object from an array of [objects] we need its indexPath value.
Rewind to line 42, I know I need the indexPath so I got it there. TAKE NOTE: tableView.indexPathForSelectedRow is a representation of the row and section of the selected cell. This line is simply giving us ACCESS to the section and row we need to carry to the next viewController.
Now, in line 44 we have to remember that we started this adventure because we needed the indexPath value, which is of type: Int. Reminder: line 44 allowed us access to the section and row, both of type: Int. Therefore, line 44 makes sets the grail variable in our detailViewController represent the grail object with the indexPath value of the selected row [indexPath.row].
In plain english, I would write line 44 as follows:
I am a representation of the screen you are going to next, and I have a variable that seems similar to one that you have, so = please allow me to access your indexPath value (Int) , so I can display some of the same information on my screen.
All that to say... just pass your data through your segue.