一部のデータを別のビューコントローラに渡す際に問題があります。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.
}
私は 'annotationTitle'何を交換するのですか? – daanyyaal
@daanyyaal編集を参照してください。 – AnthonyW