daanyyaal
daanyyaal

Reputation: 301

Swift: Pass annotation title to my second view controller label

I'm having trouble pass some data to another view controller.

I need to pass the title of an annotation to a label on my second VC.

I don't know where I'm exactly I'm going wrong, I've been scratching my head for the whole day, trying to fix it.

ViewController.swift

//Perform segue when callout has been tapped
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {

  self.performSegue(withIdentifier: "showAnnotationInfo", sender:view)

   }

I'm currently trying to use func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) but I'm getting the error: "constant 'destVC' used before being initalized".

in the didSelect function, I'm able to display the selected annotation title in the console perfectly fine.

//Idenify when callout has been selected
 func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {

        print("Annotation selected")

        if let annotation = view.annotation as? POIAnnotations {

           let destVC : ShopDetailViewController?

            //error: constant 'destVC' used before being initialized
            destVC?.shopNameData = annotation.title!

            print("Your annotation title is: \(annotation.title!)");

        }

    }

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showAnnotationInfo" {

      //I'm not sure what goes here. 

            }
        }

ShopDetailViewController.swift

Once annotation.title has been passed to shopNameData, it's passed to the label as shown below.

var shopNameData = "thisDataMustChange"

    override func viewDidLoad() {
        super.viewDidLoad()

        self.shopName.text = self.shopNameData

        // Do any additional setup after loading the view.
    }

Upvotes: 1

Views: 746

Answers (2)

Fid
Fid

Reputation: 506

Experimenting with some similar questions on SE, I came to this conclusion:

var annotationTitle = ""
func mapView(_ mapView: MGLMapView, annotation: MGLAnnotation, calloutAccessoryControlTapped control: UIControl) {
    // mapView.deselectAnnotation(annotation, animated: true) // Hide the callout view.
    annotationTitle = annotation.title!!
    performSegue(withIdentifier: "toDetail", sender: view)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if (segue.identifier == "toDetail" ) {
        print("Segue toDetail")
        let annotationTitle = self.annotationTitle
        let theDestination = segue.destination as! StationController
        theDestination.LabelText = annotationTitle
    }
}

Upvotes: 0

AnthonyW
AnthonyW

Reputation: 1998

Set your shopNameData in prepareForSegue instead.

if segue.identifier == "showAnnotationInfo" {
    guard let annotationTitle = annotationTitle else {
        print("annotation title not set before segue to ShopDetailViewController.")
        return
    }
    guard let controller = segue.destination as? ShopDetailViewController else {
        print("improper controller for this segue")
        return
    }

    controller.shopNameData = annotationTitle
}

Where annotationTitle is set when you select on the map. See below.

var annotationTitle = ""
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
  if let annotation = view.annotation as? POIAnnotations {
    annotationTitle = annotation.title!
  }
}

Upvotes: 2

Related Questions