3

我有一个带有注释的地图,单击图钉后,标注显示带有注释标题和一个披露按钮。当我点击按钮时,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)
    }
}

谢谢

4

2 回答 2

0

如果注释标题不同,您可以使用属性来MKAnnotation title查找引脚annotation

 annotationView.annotation.title

返回String。比较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)
    }
}
于 2014-10-24T14:17:29.063 回答
-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

希望这可以帮助

于 2015-09-05T19:53:55.647 回答