Swift: data is not stored in a variable when passing through VC

I am trying to save the map output name in a variable and then display it on a label on my second VC.

This is currently what I have.

FirstVC.swift

//Perform segue when callout has been tapped func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { self.performSegue(withIdentifier: "showAnnotationInfo", sender:view) } func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { print("Annotation selected") if let annotation = view.annotation as? POIAnnotations { print("Your annotation title is: \(annotation.title!)") // Instantiate ShopDetailViewController let storyboard = UIStoryboard(name: "Main", bundle: nil) let destVC = storyboard.instantiateViewController(withIdentifier: "shopDetailVC") as! ShopDetailViewController destVC.shopNameData = annotation.title! print("Shop name data has the value: \(destVC.shopNameData)") } } 

ShopDetailViewController.swift

 class ShopDetailViewController: UIViewController { @IBOutlet weak var shopName: UILabel! var shopNameData = "data" override func viewDidLoad() { super.viewDidLoad() self.shopName.text = self.shopNameData print(shopNameData) } } 

What happens when I try to save annotation.title to shopNameData , no problem.

However, when I click on the annotation, the data from annotation.title no longer stored in shopNameData and just displays the default data.

enter image description here

I'm not sure where I made a mistake here.

EDIT

I have included where I represent ShopDetailViewController .

+5
source share
2 answers

You simply create an instance of ShopDetailViewController by setting a value and then doing nothing with it. The instance of ShopDetailViewController that you created goes out of scope and freed.

How do you represent ShopDetailViewController ? Do you use segue? If you are then you must override the prepareForSegue() method, where you will get an instance of ShopDetailViewController that will be presented, and not go out of scope, and then just set the shopNameData property shopNameData .

EDIT:

Thanks for explaining how you represent the view controller. Therefore, to answer your comment:

You need to go to FirstVC and add something like the following code:

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if let viewController = segue.destination as? ShopDetailViewController, let annotationView = sender as? MKAnnotationView, let annotation = annotationView.annotation as? POIAnnotations { viewController.shopNameData = annotation.title } } 

Then after that you can simply delete the code in your func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) method func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) and save the code in the func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) method func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) .

This way you simply call the present and pass the MKAnnotationView through the sender, and then take out the relevant information when the segue prepares to present your view controller. Hope this makes sense.

+2
source

Override for the segue method as follows:

  override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if (let view = sender as! MKAnnotationView) && (segue.identifier == "showAnnotationInfo") { if annotation = view.annotation as? POIAnnotation { let destVC = segue.destination as! ShopDetailViewController destVC.shopNameData = annotation.title! } } } 
0
source

Source: https://habr.com/ru/post/1261708/


All Articles