2017-02-15 1 views
0

App Download Breite und Länge vom Server auf dem 'moreViewController' Ich habe Label und wenn ich pin pin Wie kann ich die richtige Breite oder Länge (und Bild in eine andere Ansicht) zu diesem Etikett zu transformieren?MKMapViewDelegate + JSON ALAMOFIRE

{ 
    "latitude": 40.9233, 
    "longitude": 20.000, 
    "image" : "xxxx.xxx/xxx.jpg" 
} 
{ 
    "latitude": 50.9233, 
    "longitude": 19.320, 
    "image" : "xxxx.xxx/yyy.jgp" 
} 

So lade ich es und var test = [String:AnyObject]()

Alamofire.request("website").responseJSON { response in 

    print(response.result) // result of response serialization 
    let json = response.result.val 
    print("JSON: \(json)" 
    let jsonTab = json as? Array<Any> 
     for item in jsonTab as! [AnyObject] { 
      self.test["lat"] = item["lat"] as? Double! as AnyObject? 
      self.test["lng"] = item["lng"] as? Double! as AnyObject? 
      self.addPin(lat: self.test["lat"] as! Double, lng: self.test["lng"] as! Double) 

zur globalen speichern und fügen Sie Stift

func addPin(lat:Double, lng:Double) { 
     let testPin = CLLocationCoordinate2D(latitude: lat, longitude: lng) 
     let dropPin = MKPointAnnotation() 
     dropPin.coordinate = testPin 
     dropPin.title = "test" 
     mapView.addAnnotation(dropPin) 
    } 

jetzt rechte Taste hinzufügen und segue zur nächsten Ansicht

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { 
     self.performSegue(withIdentifier: "more", sender: self) 
    } 

    var annotationIdentifier = "" 
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
     var view = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) 
     if view == nil { 
      view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier) 
      view?.canShowCallout = true 
      view?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure) 
     } else { 
      view?.annotation = annotation 
     } 
     return view 
    } 

    var selectedAnnotation: MKPointAnnotation! 
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 

     if segue.destination is moreViewController { 
      print("123") 
      let destViewController:moreViewController = segue.destination as! moreViewController 
      destViewController.lat = "\(latTest)" 

     } 
    } 

Antwort

1

Mit performSegue mit dem Sender anstelle der Übergabe der Stromreglerreferenz übergeben Sie die Anmerkung der ausgewählten Ansicht.

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { 
    self.performSegue(withIdentifier: "more", sender: view.annotation) 
} 

Jetzt in prepareForSegue Methode Zugriff auf diese Eigenschaft sender und erhalten die Anmerkung von ihm.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 

    if segue.destination is moreViewController { 

     let destViewController:moreViewController = segue.destination as! moreViewController 
     if let annotation = sender as? MKAnnotation { 
      destViewController.lat = "\(annotation.coordinate.latitude)" 
      destViewController.long = "\(annotation.coordinate.longitude)" 
     } 

    } 
} 

Hinweis: Sie haben lat und long Wert im Test Wörterbuch festgelegt, so wird es zu schreiben über den Wert und es wird enthält nur letztes lat und long Wert der Array-Objekts.

+0

Danke funktioniert perfekt :)! – k0le

+0

@ k0le Willkommen Kumpel :) –

+0

Sorry, aber ich habe eine andere Frage, die ich nicht sehen, aber diese "Ort" haben einen eigenen Namen als String und ID als String auf JSON Wie kann ich es mit Lat transportieren und lange korrekt? – k0le