Passing Data Around

When a row in the table view is tapped, you need a way of telling the DetailViewController which item was selected. Whenever a segue is triggered, the prepare(for:sender:) method is called on the view controller initiating the segue. This method has two arguments: the UIStoryboardSegue, which gives you information about which segue is happening, and the sender, which is the object that triggered the segue (a UITableViewCell or a UIButton, for example).

The UIStoryboardSegue gives you three pieces of information: the source view controller (where the segue originates), the destination view controller (where the segue ends), and the identifier of the segue. The identifier lets you differentiate segues. Let’s give the segue a useful identifier.

Open Main.storyboard again. Select the show segue by clicking on the arrow between the two view controllers and open the attributes inspector. For the identifier, enter showItem (Figure 13.15).

Figure 13.15  Segue identifier

Screenshot shows the attributes inspector.

With your segue identified, you can now pass your Item instances around. Open ItemsViewController.swift and implement prepare(for:sender:).

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // If the triggered segue is the "showItem" segue
    switch segue.identifier {
    case "showItem"?:
        // Figure out which row was just tapped
        if let row = tableView.indexPathForSelectedRow?.row {

            // Get the item associated with this row and pass it along
            let item = itemStore.allItems[row]
            let detailViewController
                    = segue.destination as! DetailViewController
            detailViewController.item = item
        }
    default:
        preconditionFailure("Unexpected segue identifier.")
    }
}

You learned about switch statements in Chapter 2. Here, you are using one to switch over the possible segue identifiers. Because the segue’s identifier is an optional String, you include a ? after the case pattern (i.e., after "showItem"). The default block uses the preconditionFailure(_:) function to catch any unexpected segue identifiers and crash the application. This would be the case if the programmer either forgot to give a segue an identifier or if there was a typo somewhere with the segue identifiers. In either case, it is the programmer’s mistake, and using preconditionFailure(_:) can help you identify these problems sooner.

Build and run the application. Tap on a row and the DetailViewController will slide onscreen, displaying the details for that item. (You will fix the inability to go back to the ItemsViewController in Chapter 14.)

Many programmers new to iOS struggle with how data is passed between view controllers. Having all of the data in the root view controller and passing subsets of that data to the next UIViewController (as you did in this chapter) is a clean and efficient way of performing this task.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset