H Patel H Patel - 1 year ago 215
Swift Question

Swift: MapKit - Recognize Pin Tap

I must be missing something obvious here.....

Add a bunch of MKPointAnnotations from Core Data...shows on Map

Does Not Work:
Try to have a print statement appear when a pin is tapped.

I have the MKMapView Delegate set and did not implement AnnotationView since I wish to segue immediately after a pin is tapped. Would rather not show a context dialog.

What am I missing to recognize a pin is tapped?

import UIKit
import MapKit
import Foundation
import CoreData

class MapViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var mapOutlet: MKMapView!

var latitude = 100.0 as Double
var longitude = 120.0 as Double

var pins = [NSManagedObject]()

func pullPins() {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext

let fetchRequest = NSFetchRequest(entityName: "Pin")

do {
let results = try managedContext.executeFetchRequest(fetchRequest)
pins = results as! [NSManagedObject]

for pin in pins {
let pinLat = pin.valueForKey("latitude") as! Double
let pinLon = pin.valueForKey("longitude") as! Double

let pinCoordinate = CLLocationCoordinate2D(latitude: pinLat, longitude: pinLon)

let annotation = MKPointAnnotation()
annotation.coordinate = pinCoordinate

catch let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")

func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
print("Pin Tapped")

override func viewDidLoad() {


Answer Source

If you are not receiving a map view delegate message, it is because you have not made yourself the map view's delegate. For example:

self.mapOutlet.delegate = self
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download