【问题标题】:Presenting a detail view controller from a map annotation in swift 3?在 swift 3 中从地图注释中呈现详细视图控制器?
【发布时间】:2016-10-24 14:58:59
【问题描述】:

我在这个主题上研究了将近 3 天,试图通过单击我的地图视图控制器中的地图注释的信息按钮来弄清楚如何呈现我的详细视图控制器。基本上,我可以显示注释,但是当我单击注释时,什么也没有发生。我想让它显示我的详细视图控制器,就像在我的表视图控制器中单击同一项目时,它直接进入其各自的详细视图一样。

任何帮助将不胜感激! 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()

【问题讨论】:

  • 绑定的 segue 是存在 segue 类型的吗?
  • 正确。只是当我按下详细信息披露按钮时没有任何反应。

标签: ios swift xcode swift3


【解决方案1】:

我相信问题可能出在calloutAccessoryControlTapped 的方法签名中。应该是:

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

【讨论】:

  • 通过删除 !从 UIControl 中,详细信息披露按钮仍然没有激活并继续到 PancakeHouseViewController
  • 您是否在mapView 参数之前添加了_
  • 啊哇!有效!!但它只为我提供了默认的 PancakeHouseViewController……而不是链接到每个特定煎饼屋的那个。
  • 您必须在prepareForSegue 方法中设置PancakeHouseViewController
  • func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if (segue.identifier == "moreDetail") { // 将数据传递给下一个视图 let destViewController:PancakeHouseViewController = segue.destination as! PancakeHouseViewController destViewController.viaSegue = sender as! MKAnnotationView } }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多