【问题标题】:Swift - passing data from mapkit annotation to another view by disclosure buttonSwift - 通过显示按钮将数据从 mapkit 注释传递到另一个视图
【发布时间】:2014-12-20 09:32:04
【问题描述】:

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

谢谢

【问题讨论】:

  • 在 calloutAccessoryControlTapped 中,annotationView.annotation 会给你注解对象。调用 performSegue 时,将 sender 设置为 annotationView 而不是 self。请参阅stackoverflow.com/questions/14805954/… 获取示例(它在 Objective-C 中,但转换为 Swift 应该不会太难)。我不推荐使用标签的方式。

标签: ios swift annotations mapkit callouts


【解决方案1】:

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

 annotationView.annotation.title

返回String。比较String进行区分。

pinview 中使用tag 属性

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

【讨论】:

    【解决方案2】:

    尝试将标题保存为 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 后,您可以像 let spotTitle = NSUserDefaults.standardUserDefaults().objectForKey("SpotTitle") as! String 那样简单地在新的“InfoView 或您所称的任何名称”中抓取对象。

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多