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 the arrow between the two view controllers and open the attributes inspector. For the identifier, enter showItem (Figure 11.15).

Figure 11.15  Segue identifier

Segue identifier

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

Listing 11.4  Injecting the selected Item (ItemsViewController.swift)

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. 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 a row and the DetailViewController will slide onscreen, displaying the details for that item. And you can dismiss the detail screen by swiping down on the interface – that makes two points in the plus column.

But there is still work to be done. One issue is that any changes that you make to the item’s details will not persist. And, in terms of style, there is a more conventional way to present and dismiss detail screens. You will address both of these issues in Chapter 12.

Many programmers new to iOS struggle with how data is passed between view controllers. Having all 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 to perform this task.

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

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