2016-12-19 5 views
1

一部のデータを別のビューコントローラに渡す際に問題があります。Swift:注釈タイトルを2番目のビューコントローララベルに渡します。

2番目のVCのラベルにアノテーションのタイトルを渡す必要があります。

私はどこが間違っているのか分かりません。私は一日中頭を傷つけて、それを修正しようとしています。

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) 

    } 

私は現在func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView)を使用しようとしているが、私はエラーを取得しています: "initalizedされる前に使用される定数 'destVC'"。

didSelect関数では、選択した注釈のタイトルをコンソールに完全に表示できます。

//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

annotation.titleがshopNameDataに渡された後、以下に示すように、それがラベルに渡されています。

var shopNameData = "thisDataMustChange" 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.shopName.text = self.shopNameData 

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

答えて

1

代わりにprepareForSegueにshopNameDataを設定してください。

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 
} 

ここで、マップ上で選択すると、annotationTitleが設定されます。下記参照。 SE上のいくつかの同様の質問を試す

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

私は 'annotationTitle'何を交換するのですか? – daanyyaal

+0

@daanyyaal編集を参照してください。 – AnthonyW

0

、私はこの結論に達した:

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 
    } 
} 
関連する問題