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

将选定注释的坐标传递给新的视图控制器Swift 2.0,swift,mapkit,viewcontroller,uistoryboardsegue,mapkitannotation,Swift,Mapkit,Viewcontroller,Uistoryboardsegue,Mapkitannotation,当点击rightCalloutAccessoryView的DetailDisclosure按钮时,我试图将所选MKAnnotation的坐标、标题和副标题从ViewController1(VC1)传递到ViewController2(VC2)。我有一个从VC1到VC2的序列,标识符为viaSegue。我在VC2中有一个标识符为viaSegueLabel的标签,我想将坐标显示为字符串 自定义MKAnnotation调出的函数,以便在rightCalloutAccessoryView中显示Detai

当点击rightCalloutAccessoryView的DetailDisclosure按钮时,我试图将所选MKAnnotation的坐标、标题和副标题从ViewController1(VC1)传递到ViewController2(VC2)。我有一个从VC1到VC2的序列,标识符为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
    }
// 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
    }
}
点击DetailDisclosure按钮时,将用户从VC1转到VC2的功能如下所示:

// 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
    }
// 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
    }
}
我认为我需要实现的功能是:

// 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
    }
// 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
    }
}

在prepareforsgue()的最后一行中,我需要引用当前选中的MKAnnotation。Swift中有没有内置的方法可以让我这样做,或者我应该将注释设置为全局的?

能够解决这个问题,以防将来有人需要实现类似的功能

self.performSegueWithIdentifier("newView", sender: view)
以编程方式将程序分段到视图控制器(VC),该视图控制器通过标识符为“newView”的分段连接到当前所在的VC。在它完全分离之前,程序调用
prepareforsgue()
。此函数用于处理将信息发送到您要访问的VC的操作。我的问题是,我不知道我到底在发送什么(在类型、变量名等方面)。如果您注意到,
prepareforsgue()
self.performsguewithidentifier(“newView”,sender:view)
都有一个参数sender。使用
performsguewithidentifier()
发送的内容将被传递到
prepareforsgue()
中,并在
destinationViewController
中被名为viaSegue的变量接收。这不是一个标准名称,它只是我选择的变量名称,如果您研究上面的代码,您将看到它在哪里使用以及如何工作

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


希望这有帮助

能够解决这个问题,以防将来有人需要实现类似的功能

self.performSegueWithIdentifier("newView", sender: view)
以编程方式将程序分段到视图控制器(VC),该视图控制器通过标识符为“newView”的分段连接到当前所在的VC。在它完全分离之前,程序调用
prepareforsgue()
。此函数用于处理将信息发送到您要访问的VC的操作。我的问题是,我不知道我到底在发送什么(在类型、变量名等方面)。如果您注意到,
prepareforsgue()
self.performsguewithidentifier(“newView”,sender:view)
都有一个参数sender。使用
performsguewithidentifier()
发送的内容将被传递到
prepareforsgue()
中,并在
destinationViewController
中被名为viaSegue的变量接收。这不是一个标准名称,它只是我选择的变量名称,如果您研究上面的代码,您将看到它在哪里使用以及如何工作

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

希望这有帮助