ios - 将数据从 Map Annotation 传递到详细 View Controller SWIFT 3

标签 ios swift mkmapview swift3 mkannotation

我在尝试弄清楚如何通过 map View Controller 中的图钉 map 注释来呈现细节 View Controller 时遇到了很多麻烦。当我单击详细信息披露按钮时,它会将我带到详细信息 View Controller ,但不会传递任何数据。这只是我在 Storyboard 中制作的基本布局。 在注释中,I only have the a title, subtitle, and location coordinates .但是在我的详细 View Controller 中,我有超过 15 个不同的数据。

我如何呈现它,以便当我单击 map 注释时,它会将我带到其各自的包含所有数据的详细信息 Controller ,而不仅仅是来自 Annotation 类的数据。

如有任何帮助,我们将不胜感激!

这是我的 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)
    }
}

我的 .viaSegue 连接到名为 PancakeHouseViewController 的详细 View Controller

class PancakeHouseViewController: UITableViewController, MKMapViewDelegate {

var viaSegue = MKAnnotationView()

@IBOutlet weak var addressLabel: UILabel!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var detailsLabel: UILabel!
@IBOutlet weak var priceGuideLabel: UILabel!

这是“注释”类

class Annotations: NSObject, MKAnnotation {

var title: String?
var subtitle: String?
var latitude: Double
var longitude: Double

var coordinate: CLLocationCoordinate2D {
    return CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
}

init(latitude: Double, longitude: Double) {
    self.latitude = latitude
    self.longitude = longitude
}

最佳答案

在您的 pancakeHouseVC 类中添加一个新字典,您将使用它来传递数据

class PancakeHouseViewController: UITableViewController, MKMapViewDelegate {

 var viaSegue = MKAnnotationView()
 var dataDict:[String:String]
  1. 在你的 prepareForSegueMethod 中

     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)
    // set your data here in dataDict
    destViewController. dataDict = ["name":"add name"]
      }
    }
    

关于ios - 将数据从 Map Annotation 传递到详细 View Controller SWIFT 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40337827/

相关文章:

JavaScript 在 Mobile Safari 中无法运行

ios - Swift 数字泛型?

iphone - 如何比较两个不同的 map View 区域并找出它们之间的差异

macos - 在 MKMapView 中检测双击鼠标事件(不是 iOS 触摸事件)

iPhone编程: weird error using a MKMapView

ios - 面部追踪器寻找眼睛 iphone

iphone - 显示 pickerview 和操作表

swift - UIViewRepresentable 没有更新绑定(bind)到 ObservedObject 属性?

ios - 如何选择注释标题?

ios - 使用 SceneKit 在 ARKit 中绘制楔形