1

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 Answers1

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)
Lu_
  • 2,577
  • 16
  • 24
  • I've been playing around with it there I can't seem to get it to work because I don't understand it. could you explain it in a little more detail please? –  Aug 02 '16 at 09:47
  • check edit, there is really nothing to understand you are making your annotation that contain " Is there a way to add extra information" that what you want – Lu_ Aug 02 '16 at 09:58
  • It was popping up as 'missing argument for parameter "coordinate" in call' earlier on. What does this mean? –  Aug 02 '16 at 22:31
  • look at initialization, it requires coordinate, you can modify that – Lu_ Aug 03 '16 at 07:12