Beginner's question: Transferring MKPointAnnotation title to a new VC

Hello,

I am a new to programming (not just to iOS, to all programming). I finished an Udemy course and am now playing around with making a simple mapkit app. I'm hanging on pretty well, but got stuck on transferring an MKPointAnnotation title property to a new VC through a segue.

Essentially, I have created a struct for my locations:

Code Block
struct Location {
let id = UUID()
let name: String
let latitude: Double
let longitude: Double
var isFavorite: Bool
var coordinate: CLLocationCoordinate2D {
CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
}
let website: String     
}


Then I've created an array and filled it with Locations:

Code Block
let places = [
Location(name: "Mordor", latitude: -41.29, longitude: 175, isFavorite: false, website: "http://www.mordor.com"),
Location(name: "Lorien", latitude: -41.31, longitude: 174, isFavorite: false, website: "http://www.lorien.com"),
]


Then I've populated my map from the array:

Code Block
fileprivate func addLocationToMap() {
for place in places {
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: place.latitude, longitude: place.longitude)
annotation.title = place.name
mapView.addAnnotation(annotation)
}
}


Everything works fine -- and more importantly, I understand everything I've done so far. Here's where I am stuck:

When I click on an annotation, a new ViewController shows up through a segue. No problem there. What I want to do is to take the title of the annotation, and make it show up as a label (which I have already created, called detailsTitleLabel, taking its title from detailsTitle: String) in the new VC to which I am segueing.

Or, to put it another way, what to I put in place of FOO below in order to transfer annotation.title of the clicked annotation (which is the same String as the "name" string from my places array) to the new VC? The code works otherwise -- if I replace FOO with a random string, it gets transferred to the new VC fine, I just don't know how to replace FOO with annotation.title.

Code Block
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
self.performSegue(withIdentifier: "toDetailsVC", sender:self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toDetailsVC" {
let destinationVC = segue.destination as? DetailsVC
destinationVC?.detailsTitle = FOO
}
}


Eventually, the destination VC will go into a navigation controller while everything else is in a tab bar controller, but that's for another time... thankful for any guidance!
Answered by OOPer in 662834022
Why don't you utilize the sender of the segue?
Code Block
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
self.performSegue(withIdentifier: "toDetailsVC", sender: view.annotation) //<-
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toDetailsVC" {
guard let destinationVC = segue.destination as? DetailsVC,
let annotation = sender as? MKPointAnnotation else {
print("Unexpected segue: \(segue), \(String(describing: sender))")
return
}
destinationVC.detailsTitle = annotation.title
}
}


Accepted Answer
Why don't you utilize the sender of the segue?
Code Block
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
self.performSegue(withIdentifier: "toDetailsVC", sender: view.annotation) //<-
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toDetailsVC" {
guard let destinationVC = segue.destination as? DetailsVC,
let annotation = sender as? MKPointAnnotation else {
print("Unexpected segue: \(segue), \(String(describing: sender))")
return
}
destinationVC.detailsTitle = annotation.title
}
}


Brilliant! I had tried playing around with sender in prepareforsegue, but didn't realize I had to do it in performSegue. Thank you!
Beginner's question: Transferring MKPointAnnotation title to a new VC
 
 
Q