在swift 3中从地图注释中呈现详细视图控制器?

时间:2016-10-24 05:40:15

标签: ios swift xcode swift3

通过点击我的地图视图控制器中地图注释的信息按钮,我已经研究了这个主题近三天,试图找出如何呈现我的详细视图控制器。基本上,我可以获得要显示的注释,但是当我单击注释时,没有任何反应。我想让它呈现我的详细视图控制器,就像在我的表视图控制器中单击相同项目时一样,它直接进入其各自的详细视图。

任何帮助都会非常多,非常感谢! This is the image of the map annotation I currently have

下面是我的MapViewController的代码。我觉得我的prepareForSegue或annotationView函数中有错误或缺少某些东西。

extension MapViewController {

  // 1
  @objc(mapView:viewForAnnotation:) func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

    if let annotation = annotation as? Annotations {

      let reuseID = "pin"

      var view: MKPinAnnotationView
      if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseID)
            as? MKPinAnnotationView { // 2
          dequeuedView.annotation = annotation
          view = dequeuedView
      } else {
        // 3
        view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseID)
        view.canShowCallout = true
        view.isUserInteractionEnabled = true
        view.calloutOffset = CGPoint(x: -5, y: 5)
        view.rightCalloutAccessoryView = UIButton.init(type: .detailDisclosure) as UIButton
      }
      return view
    }
    return nil
  }

  func mapView(mapView: MKMapView, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
    if control == view.rightCalloutAccessoryView {
      print(view.annotation?.title)
      performSegue(withIdentifier: "moreDetail", sender: self)
    }

  }


  func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "moreDetail") {
      // pass data to next view
      let destViewController:PancakeHouseViewController = segue.destination as! PancakeHouseViewController
      destViewController.viaSegue = sender as! MKAnnotationView
    }
  }

}

我还在我的详细视图控制器(PancakeHouseViewController)中包含了一个变量...我不知道它是否应该在那里。

var viaSegue = MKAnnotationView()

1 个答案:

答案 0 :(得分:0)

我认为问题可能出在calloutAccessoryControlTapped的方法签名中。它应该是:

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl)