Swift:MapKit - 识别Pin Tap

时间:2016-07-29 21:37:49

标签: ios swift

我必须遗漏一些明显的东西......

使用: 从核心数据中添加一堆MKPointAnnotations ...在地图上显示

不起作用: 点击引脚时,尝试显示打印声明。

我有MKMapView Delegate设置并没有实现AnnotationView,因为我希望在点击一个引脚后立即进行segue。宁愿不显示上下文对话框。

我认识到引脚被点击了什么?

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

                self.mapOutlet.addAnnotation(annotation)
            }
        }
        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() {
        super.viewDidLoad()

        pullPins()
    }
}

1 个答案:

答案 0 :(得分:2)

如果您没有收到地图视图委托消息,那是因为您没有自己制作地图视图的委托。例如:

self.mapOutlet.delegate = self