How to Create an Outlet for the Map Controller in Your iOS App

By Neal Goldstein, Dave Wilson

If you want to use the MapController for your iOS app, you will need an outlet. With your Map selector taken care of, it’s time to create an outlet so that MapController has access to the Map view to center the map and have the annotations display. Follow these steps:

1Close the Utility area and select the Assistant from the Editor selector on the Xcode toolbar.

If the MapController.m file isn’t the one that’s displayed, go up to the Assistant’s Jump bar and select it.

2Control-drag from the Map view in the Canvas or the Document Outline to the MapController class extension (the private interface at the top of the file) and create an IBOutlet named mapView.

You’ll notice a compiler (Live Issue) error— Unknowntype name ’ MKMapView ’.

3Because you’ll be using the MapKit framework and have a property of type MKMapView, you need to update the MapController public interface (in the MapController.h file) with the bolded code that follows.

You have it adopt the MKMapViewDelegate protocol as well.

4Select and then Control-drag from the segmented control to the MapController private interface between the @intrface and –d statements.

In this case, it is easier to select and drag from the Document Outline.

5In the dialog that appears, choose Action from the Connection drop-down menu.

In the same dialog, leave Value Changed as the selection in the Event drop-down menu.

Value Changed is the event that is generated when the user touches a segment to change the selection.

6Still in the same dialog, name this action mapType by entering mapType in the Name field; then click Connect.

This action will provide to your code the type of map selected by the user making a choice using the segmented control.

Because you now have an outlet set up, you’ll make the Map controller a delegate in your code as opposed to doing it in Interface Builder as you have been doing previously. Either way is fine.

#import "DetailViewController.h"
#import <MapKit/MapKit.h>
@interface MapController : RTDetailViewController