夫特 - 通过公开按钮从mapkit注释将数据传递到另一个视图

问题描述 投票:3回答:2

我有注释的地图,点击PIN后,标注显示了注释的标题和披露按钮。当我点击按钮SEGUE被触发,我移动到另一个视图。如何确定单击什么注释,或标题传递到另一个视图。

func mapView(mapView: MKMapView!,
    viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
        if annotation is MKUserLocation{
            return nil
        }

        let reuseId = "pin"

        var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView

        if(pinView == nil){
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            pinView!.canShowCallout = true
            pinView!.animatesDrop = true
            pinView!.pinColor = .Red

            var calloutButton = UIButton.buttonWithType(.DetailDisclosure) as UIButton
            pinView!.rightCalloutAccessoryView = calloutButton
        } else {
            pinView!.annotation = annotation
        }
        return pinView!
}

func mapView(mapView: MKMapView!, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {

    if control == annotationView.rightCalloutAccessoryView {
        performSegueWithIdentifier("Detail", sender: self)
    }
}

谢谢

ios swift annotations mapkit callouts
2个回答
0
投票

您可以使用MKAnnotation title财产annotation寻找引脚如果注释标题是不同的

 annotationView.annotation.title

返回String.Compare String者区分。

要么

使用在tag pinview财产

pinView!.tag //set tag for each pin in `viewForAnnotation` method

func mapView(mapView: MKMapView!, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {

    //get tag here
      if(annotationView.tag == 0){
        //Do for 0 pin
     }

    if control == annotationView.rightCalloutAccessoryView {
        performSegueWithIdentifier("Detail", sender: self)
    }
}

-1
投票

请尝试保存标题给你然后NSUserDefaults的抓住该对象在新的视图。这是我使用的方法。

 func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
    if control == view.rightCalloutAccessoryView{


        println(view.annotation.title) // annotation's title

        let title = view.annotation.title


        NSUserDefaults.standardUserDefaults().setObject(title, forKey: "Title")


        var InfoView = self.storyboard?.instantiateViewControllerWithIdentifier("Spot") as! UIViewController

一旦保存标题到你NSUserDefaults的可以简单地抢在新“的InfoView或任何你在呼唤它”像这样let spotTitle = NSUserDefaults.standardUserDefaults().objectForKey("SpotTitle") as! String对象

希望这可以帮助

© www.soinside.com 2019 - 2024. All rights reserved.