2015-09-05 62 views
1

我很難將選定註釋的標題傳遞給segued視圖控制器。我覺得這可能很簡單,我需要改變,但無法弄清楚。將註釋標題傳遞給視圖控制器(swift)

這裏是準備SEGUE代碼:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if(segue.identifier == "viewDetail"){ 

     let theDestination : DetailViewController = segue.destinationViewController as! DetailViewController 
     theDestination.getName = view.annotation.title! 
    } 
} 

,這裏是代碼註釋時,召喚出被竊聽,它執行SEGUE

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) { 
    self.performSegueWithIdentifier("viewDetail", sender: self) 

} 

的問題是, ,在prepareForSegue函數..它不識別「view.annotation.title!」聲明UIView沒有成員「註釋」。

我知道當我在其他功能調用println(view.annotation.title!),它的工作原理

感謝您的幫助

回答

1

你需要傳遞viewperformSegueWithIdentifiersender參數地圖查看委託方法。

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) { 
    self.performSegueWithIdentifier("viewDetail", sender: view) 
} 

之後,您可以在prepareForSegue執行讀出來的sender參數。

annotation = self.map.selectedAnnotations[0] as! MKAnnotation 

,以便它發送在視圖中的第一選擇的註釋,這是唯一的one..the註釋標題:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if(segue.identifier == "viewDetail"){ 
     let theDestination : DetailViewController = segue.destinationViewController as! DetailViewController 
     theDestination.getName = (sender as! MKAnnotationView).annotation!.title! 
    } 
} 
0

感謝您的幫助,我用一個簡單的線解決了它一個我叫。

+0

你把這條線放在哪裏?我正在嘗試做同樣的事情,但無法弄清楚 – cfsprod

相關問題