Creating MapViewController

In Chapter 11, Finishing Up Your App Structure in Storyboard, you added a map view to the Map screen. In the previous sections, you added MapLocations.plist to your project and created a new class, RestaurantItem, and a data manager class, MapDataManager. Remember the MVC design pattern? At this point, you have created the views and models for the Map screen, and now you will create the controller.

A view controller managing a map view can provide objects conforming to MKAnnotation for it. It is also usually the delegate for the map view and will provide MKAnnotationView objects for MKAnnotation objects visible onscreen. You will now implement a view controller for the map view named MapViewController. Perform the following steps:

  1. Right-click the Map folder and select New File.
  2. iOS should already be selected. Choose Cocoa Touch Class and then click Next.
  3. Configure the file as follows:
  • Class: MapViewController
  • Subclass: UIViewController
  • Also create XIB: Unchecked
  • Language: Swift
  • Click Next
  1.  Click Create. MapViewController.swift appears in the Project navigator.
  2. In MapViewController.swift, under import UIKit, add import MapKit. This tells Xcode you will be using the MapKit framework.
  3. Modify the class declaration as follows:
class MapViewController: UIViewController, MKMapViewDelegate {

This makes MapViewController conform to the MKMapViewDelegate protocol. Now that you have the MapViewController class, you need to assign it to the view controller in the view controller scene for the Map screen and create an outlet for the map view. 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
18.116.8.110