2015-09-05 63 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

相关问题