T. Kearsley T. Kearsley - 23 days ago 19
Swift Question

Pass Data from View Controller MapKit

I realise there are solutions for passing data from view controllers. However, the problem I am having is I am using Map Kit and I am unsure which Pin will be clicked on it could be one of the following:

artworkPin = Artwork(title:"Wind Wand",locationName:"Majestic",discipline:"Statue",
coordinate:windwandcoord)
artworkPin2 = Artwork(title:"Wind Wand2",locationName:" Not Majestic",discipline:"Statue",
coordinate:windwandcoord2)


I want the label on the ViewTwo (second view controller) to appear as the title of the pin's "info" button that was clicked on. I currently have it setup as:
var artworkPin: Artwork!

override func viewDidLoad() {
super.viewDidLoad()
art_title.text = artworkPin.title


which only loads the label as the title of artworkPin ( the first pin ).
attached code if necessary:
ViewTwo
ViewControllerOne

Thanks for your help.

Answer

However, the problem I am having is I am using Map Kit and I am unsure which Pin will be clicked on

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    if control == view.rightCalloutAccessoryView {
        if let artworkPin = view.annotation as? Artwork {
            performSegue(withIdentifier: "no", sender: artworkPin)
        }
    }
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let identifier = segue.identifier {
        if identifier == "no" {
            if let artworkPin = sender as? Artwork {
                let ViewTwo = segue.destination as! ViewTwo
                ViewTwo.artworkPin = artworkPin
            }
        }
    }
}
Comments