【问题标题】:Passing coordinates of selected Annotation to new View Controller Swift 2.0将选定注释的坐标传递给新的 View Controller Swift 2.0
【发布时间】: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


    【解决方案1】:

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

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

    通过标识符为"newView" 的segue 以编程方式将您的程序与连接到您当前所在的VC 的视图控制器(VC) 连接。在它完全分离之前,程序调用prepareForSegue()。这个函数是你将信息发送到你正在连接的 VC 的地方。我的问题是,我不确切知道我发送的是什么(在类型、变量名等方面)。如果您注意到,prepareForSegue()self.performSegueWithIdentifier("newView", sender: view) 都有一个参数 sender。您使用performSegueWithIdentifier() 发送的内容将被传递到prepareForSegue(),并通过名为viaSegue 的变量在您的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 是如何指定为将接收此对象的变量。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-29
      • 2019-06-15
      • 1970-01-01
      • 2015-04-03
      • 2014-09-05
      • 1970-01-01
      相关资源
      最近更新 更多