Performing the showDetail segue

You have linked the map view controller scene inside Main.storyboard to a restaurant detail scene inside RestaurantDetail.storyboard using a storyboard reference and you have set the segue identifier to showDetail. You now need to implement a method to perform that segue and assign it to the callout button. Before you do so, you'll use an enumeration (discussed in Chapter 7, Classes, Structures, and Enumerations) to hold all of the segue identifiers for this project. This reduces the chances of making a mistake by enabling autocompletion when you type the segue identifiers later in your code:

  1. Right-click on the Misc folder inside the LetsEat folder and choose New File.
  2. iOS should already be selected. Choose Swift File and then click Next.
  3. Name this file Segue. Click Create.
  4. Add the following under the import statement to declare the Segue enumeration:
enum Segue:String {
case showDetail
case showRating
case showReview
case showAllReviews
case restaurantList
case locationList
case showPhotoReview
case showPhotoFilter
}

Eventually, you will need all these segue identifiers, so you add them now. Note that Segue is of type String, so the raw values for each case are strings. For example, the raw value for case showDetail is "showDetail".

Now you can add the method to perform the showDetail segue when the callout button is tapped.

  1. Click MapViewController.swift in the Project navigator.
  2. Add the following method under the addMap(_:) method:
func mapView(_ mapView: MKMapView, annotationView view: 
MKAnnotationView, calloutAccessoryControlTapped control:
UIControl){
self.performSegue(withIdentifier: Segue.showDetail.rawValue,
sender: self)
}

mapView(_:annotationView:calloutAccessoryControlTapped:) is another method specified in the MKMapViewDelegate protocol. It is triggered when the user taps the callout button. self.performSegue(withIdentifier: Segue.showDetail.rawValue, sender: self) performs the segue with the "showDetail" identifier, which makes the Restaurant Detail screen appear.

Build and run your project. On the Map screen, tap an annotation:

Tap the callout button inside the annotation:

The new Restaurant Detail screen appears, but it does not contain any details about the restaurant: 

You will make the Restaurant Detail screen display the details of a restaurant in the next chapter, but for now, let's just pass the data about the selected restaurant to the Restaurant Detail screen's view controller and print it to the Debug area. You will do this in the next section.

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

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