Different info for different pin annotations

63 Views Asked by At

I have two different pins placed on my mapview. I have an info button on each. The info buttons will segue to the a UIViewController that has a Image view (to hold a picture of the place) and a Text label ( To hold info about the place).

My problem is how can I generate the Info and picture depending on which pin annotation button was selected. The last function is the one used in order to segue to the info view controller.

  class GetToTheStart: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
//map view outlet
@IBOutlet weak var mapView: MKMapView!

//defining use of location manager
let myLocMgr = CLLocationManager()




     override func viewDidLoad() {
    super.viewDidLoad()

    //setting up location request
    myLocMgr.desiredAccuracy = kCLLocationAccuracyBest
    myLocMgr.requestWhenInUseAuthorization()
    myLocMgr.startUpdatingLocation()
    myLocMgr.delegate = self
    mapView.delegate = self

    // coordinates of desired locations for pins 
    var zoo1 = CLLocationCoordinate2DMake(53.347439, -6.291820)
    var town1 = CLLocationCoordinate2DMake(53.347247, -6.290865)

    //setting up pin 1 annotation (the zoo) 
    var zoopin = MKPointAnnotation()
    zoopin.coordinate = zoo1
    zoopin.title = "Dublin Zoo"
    zoopin.subtitle = "This this the zoo"
    mapView.addAnnotation(zoopin)

    //setting up pin 2 annotation (the town) 
    var townpin = MKPointAnnotation()
    townpin.coordinate = zoo1
    townpin.title = "Dublin town"
    townpin.subtitle = "This this the town"
    mapView.addAnnotation(townpin)
 }


   //setting up Pin callout button for segue
   func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation {
    }
    let reuseIdentifier = "pin"
    var pin =    mapView.dequeueReusableAnnotationViewWithIdentifier(reuseIdentifier) as? MKPinAnnotationView
    if pin == nil {
        pin = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
        pin!.pinColor = .Red
        pin!.canShowCallout = true
        pin!.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure)
    } else {
        pin!.annotation = annotation
    }
    return pin
}



   //performing segue from info button to infoViewController
   func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    performSegueWithIdentifier("info", sender: view)
}
1

There are 1 best solutions below

4
Naveen Kumar H S On

For this you need to override below method. Here we will get the annotationView which will trigger the segue.

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

And in the detailViewController is same as your infoViewController and here I have two labels and for that I have two public variables. This is just to avoid error because at this point we don't have the label objects.

Here is the code for my DetailViewController.

import UIKit

class DetailViewController: UIViewController {

@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var detailLabel: UILabel!


var titleText: String? { didSet { updateUI() } }
var detaileText: String? { didSet { updateUI() } }

override func viewDidLoad() {
    super.viewDidLoad()
    updateUI()
}

private func updateUI() {
    self.titleLabel?.text = self.titleText
    self.detailLabel?.text = self.detaileText
}
}