1

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

自定义 MKAnnotation 的调用以在 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 设为全局?

4

1 回答 1

0

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

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

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

所以我想发送关于我点击的 MKAnnotation 的信息。因此,我需要将一个 MKAnnotationView 类型的对象发送到我的接收 VC“BusStopSettingsViewController”(BSSVC)。在 BSSVC 中,我需要一个 MKAnnotationView 类型的名为“viaSegue”的变量。要向 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指定为将接收此对象的变量。

希望这可以帮助!

于 2016-01-23T23:38:37.743 回答