Add extra detail to MKPointAnnotation other than title and subtitle

混江龙づ霸主 提交于 2019-12-11 04:31:26

问题


I wish to pass informations from a Pin annotation to another viewController.I can pass the title and subtitle of the annotation but I need to pass some extra information along with these. Is there a way to add extra information to a MKPointAnnotation other than just title and subtitle?

here I have the pin title and subtitle set so it appears on the map:

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

the title and subtitle are then passed to my info view controller using:

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:


make your own annotation, new file or class

import MapKit

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

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

and use it instead of normal 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)


来源:https://stackoverflow.com/questions/38716410/add-extra-detail-to-mkpointannotation-other-than-title-and-subtitle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!