将所选注释的坐标传递给新的视图控制器 Swift 2.0

Passing coordinates of selected Annotation to new View Controller Swift 2.0

我试图在点击 rightCalloutAccessoryView 的 DetailDisclosure 按钮时将所选 MKAnnotation 的坐标、标题和副标题从 ViewController1 (VC1) 传递到 ViewController2 (VC2)。我有一个从 VC1 到 VC2 的 segue,标识符为 viaSegue。我在 VC2 中有一个带有标识符 viaSegueLabel 的标签,我想在其中将坐标显示为字符串。

自定义 MKAnnotation 的调用以使其在右侧的 CalloutAccessoryView 中显示 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 设为全局?

能够弄清楚以防将来有人需要实施类似的东西。

self.performSegueWithIdentifier("newView", sender: view)

以编程方式将您的程序连接到视图控制器 (VC),该视图控制器连接到您当前通过标识符 "newView" 进入的 VC。在它完全 segues 之前,程序调用 prepareForSegue()。这个函数是你将处理发送信息到你正在继续的 VC 的地方。我的问题是,我不知道我发送的到底是什么(在类型、变量名等方面)。如果您注意到,prepareForSegue()self.performSegueWithIdentifier("newView", sender: view) 都有一个参数 sender。您使用 performSegueWithIdentifier() 发送的内容将被传递到 prepareForSegue() 并在您的 destinationViewController 中由名称为 viaSegue 的变量接收。这不是标准名称,这只是我选择命名该变量的名称,如果您研究上面的代码,您将看到它的使用位置和工作原理。

所以我想发送有关我点击的 MKAnnotation 的信息。因此,我需要将 MKAnnotationView 类型的对象发送到我的接收方 VC "BusStopSettingsViewController" (BSSVC)。在 BSSVC 中,我需要一个名为 "viaSegue" 的 MKAnnotationView 类型的变量。要向 BSSVC 发送一个 MKAnnotationView 类型的对象,我需要做

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    if (segue.identifier == "newView") {
        // pass data to next view
        let destViewController:BusStopSettingsViewController = segue.destinationViewController as! BusStopSettingsViewController
        destViewController.viaSegue = sender as! MKAnnotationView
    }
}

注意如何将 viaSegue 指定为将接收此对象的变量。

希望对您有所帮助!