0

I have a mapView in one view controller with an MKPointAnnotation. I've applied reverse Geocoder to get the street name from the coordinates and I'm trying to take that street name onto another view controller and place it into a label. But that label from the second view controller always shows the coordinates 0.0, instead of the street name.

func configureAddressNameTry() {
    let vc = ViewController()
    
    let lat = vc.annotation.coordinate.latitude
    let long = vc.annotation.coordinate.longitude

    let geocoder = CLGeocoder()
    geocoder.reverseGeocodeLocation(CLLocation(latitude: lat, longitude: long)) { (placemarks, error) in
        if let places = placemarks {
            for place in places {
                
                // This is the line that gives me the coordinates 0.0 (North Atlantic Ocean)
                self.addressNameTry.text = place.name
            }
        }
    }

I think I have all the necessary code for the user location and the information in the info.plist, so I don't know what I'm missing. I'm new to programming and I hope I'm not making a basic error.

Thank you for your replies :)

danicode
  • 25
  • 4
  • `let vc = ViewController()` is a local variable inside `configureAddressNameTry` this is a brand new completely newly initialized instance of `ViewController` not the one that actually was loaded on screen. you need to pass the data from the ViewController instance which actually was loaded and showed annotation, use segue delegates if you are using segue to transition from one screen to another or implement simple delegate pattern to communicate between two view controllers and pass data from one view controller to another while you either push / present this screen. – Sandeep Bhandari Feb 10 '21 at 16:07
  • Does this answer your question? [IOS Swift, delegate to communicate between two view controllers](https://stackoverflow.com/questions/43239688/ios-swift-delegate-to-communicate-between-two-view-controllers) – Sandeep Bhandari Feb 10 '21 at 16:08

0 Answers0