除了标题和副标题之外,还向MKPointAnnotation添加额外的细节

时间:2016-08-02 09:17:20

标签: swift mkmapview mkannotation mkannotationview mkpointannotation

我希望将Pin注释中的信息传递给另一个viewController。我可以传递注释的标题和副标题,但我需要传递一些额外的信息。有没有办法将额外的信息添加到MKPointAnnotation而不仅仅是标题和副标题?

这里我设置了标题和副标题,因此它出现在地图上:

    var zoopin = MKPointAnnotation()
    zoopin.coordinate = zoo
    zoopin.title = "The zoo"
    zoopin.subtitle = "hello this is the zoo"
    mapView.addAnnotation(zoopin)

然后使用以下标题将字幕和副标题传递给我的信息视图控制器:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "info") {
        if let annotation = sender as? MKAnnotationView {
            let detailViewController = segue.destinationViewController as! info
            detailViewController.titleText  = annotation.annotation?.title ?? ""
            detailViewController.detaileText = annotation.annotation?.subtitle ?? ""

        }
    }
}

1 个答案:

答案 0 :(得分:1)

制作自己的注释,新文件或类

import MapKit

class MyAnnotation: NSObject, MKAnnotation {
    var coordinate: CLLocationCoordinate2D
    var EXTRA_INFORMATION: String?
    var title: String?

    init(coordinate: CLLocationCoordinate2D) {
        self.coordinate = coordinate
    }
}

并使用它代替普通的MKPointAnnotation

var zoopin = MyAnnotation()
zoopin.coordinate = zoo
zoopin.title = "The zoo"
zoopin.subtitle = "hello this is the zoo"
zoopin.EXTRA_INFORMATION = "that is your new extra info that you wanted to add?"
mapView.addAnnotation(zoopin)