1

我希望将信息从 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 ?? ""

        }
    }
}
4

1 回答 1

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)
于 2016-08-02T09:19:37.870 回答