【发布时间】:2016-01-23 07:53:40
【问题描述】:
当点击 rightCalloutAccessoryView 的 DetailDisclosure 按钮时,我试图将所选 MKAnnotation 的坐标、标题和副标题从 ViewController1 (VC1) 传递到 ViewController2 (VC2)。我有一个从 VC1 到 VC2 的 segue,标识符为 viaSegue。我在 VC2 中有一个带有标识符 viaSegueLabel 的标签,我想将坐标显示为字符串。
自定义 MKAnnotation 的 callout 使其在 rightCalloutAccessoryView 中显示 DetailDisclosure 按钮的函数如下所示:
// Customize Annotation Callout
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
// 1
let identifier = "Capital"
// 2
if annotation.isKindOfClass(Capital.self) {
// 3
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
if annotationView == nil {
//4
annotationView = MKPinAnnotationView(annotation:annotation, reuseIdentifier:identifier)
annotationView!.canShowCallout = true
// 5
let btn = UIButton(type: .DetailDisclosure)
annotationView!.rightCalloutAccessoryView = btn
} else {
// 6
annotationView!.annotation = annotation
}
return annotationView
}
// 7
return nil
}
点击 DetailDisclosure 按钮时将用户从 VC1 带到 VC2 的函数如下所示:
// When righCalloutAccessoryView is tapped, segue to newView
func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
self.performSegueWithIdentifier("newView", sender: view)
}
我认为我需要实现的功能如下:
// Pass data to newView
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "newView") {
let destViewController:BusStopSettingsViewController = segue.destinationViewController as! BusStopSettingsViewController
destViewController.viaSegue = // not sure how to reference selected Annotation here
}
}
在prepareForSegue()的最后一行,我需要引用当前选中的MKAnnotation。 Swift 中是否有内置的方法可以让我这样做,还是应该让 Annotation 全局化?
【问题讨论】:
标签: swift mapkit viewcontroller uistoryboardsegue mapkitannotation