Bootcamp, iOS Development, Journey, Lambda School, Swift

iOS Development – Perform Segue

There is a way to go from one screen to another in iOS development and I get the concept and I have seen it done. But I am struggling with the code for it. It seem like it’s just a bunch of mumbo jumbo. Words thrown together and then poof it works. There has got to be a way of making sense of it all and I intend to find out and hopefully I will be able to understand it instead of just copying and pasting code.

Let’s dive into this journey together and see if we can make it all make sense.

STEP 1: Create a new iOS Project

 

STEP 2: Build the storyboards

  1. I’m going to keep it simple so I will have a navigation view controller, a UITableViewController and a UIViewController
  2. I will create a name for the cell and a name for the segue itself.

STEP 3: Creating the swift files for the code

  1. We want to create two new Cocoa Touch Classes, One for the table view controller and the other one for the view controller

STEP 4: Linking the controllers with the code files

  1. The code needs to be attached to the view controllers and we do this in the storyboard.
  2. We need to add a label to the View Controller so that we can pass in the data from the table view controller to the view controller.

STEP 5: Adding the Code

    1. The first thing I want to do is add an array that will hold some names
    2. The first function (numberOfRowsInSection) should return the amount of names we have in our array. So we do
      return names.count

      This will change dynamically if we add or subtract items from the array.

    3. Now this is where things get a bit challenging and not so straight forward. This function will run as many times as there is data in our array.
      1. We need to dequeue the cell : This means we are going to create the cell from our storyboard so we have to put the name in the ” “. The line of code is already provided for us by Xcode. Thank goodness for that we just change the text in between the parentheses.
        let cell = tableView.dequeueReusableCell(withIdentifier: "NameCell", for: indexPath)
      2. Get the right Name for the index path: What we are doing here is that we are creating a variable to hold the data from the cell that was clicked on. 
        let name = names[indexPath.row]
      3. Then set the cell’s NameLabel‘s text to the name we got.
        cell.textLabel?.text = name

Screen Shot 2020-01-29 at 6.46.30 PM

 

When we run our app we should see all of our names. Each name is on its own line. We can click on a name but all it will do is take us to our view controller. We need to pass in information to it, but before we can pass information, we need to have a place to store it.

STEP 6: Creating our NameDetailViewController File

    1. For this all we need to do is to create a variable that will hold the data from our array. To make it easy to remember we can call it the same thing name
    2. The second step is to assign it to our label

STEP 7: Connecting our Segue and pass the data

Now that we have our view controllers all set up we can finally try to understand what is happening and what we do to pass information from one segue to another.

  1. There can only be one prepare for segue function, it’s a good idea to check the name of the segue in case there are multiple segues in our app.
  2. We need to check to see if the view controller we are going to show is the view controller we want to use and we do this with a if let statement because it might or might not be correct.
  3. If everything is set up correctly then we need to create an variable for the indexPath (the row that was clicked on)
  4. Just like before we create a variable of the data we are going to pass in.
  5. Now we need to send the variable to the view controller

Now our app is complete and I think that the perform segue function makes more sense but the only way to truly understand it is to just keep practicing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s