将数据从Map Annotation传递到详细视图控制器SWIFT 3

时间:2016-10-31 07:05:36

标签: ios swift mkmapview swift3 mkannotation

我在试图弄清楚如何从地图视图控制器中的引脚图注释中呈现我的详细视图控制器时遇到了很多麻烦。当我单击详细信息显示按钮时,它将转到详细视图控制器,但不会传递任何数据。这只是我在故事板中制作的基本布局。 在注释I only have the a title, subtitle, and location coordinates中。但在我的详细视图控制器中,我有超过15种不同的数据。

如何呈现它,以便当我点击地图注释时,它将带我到各自的细节控制器,包含所有数据,而不仅仅是来自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

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
}

}

1 个答案:

答案 0 :(得分:0)

在你的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"]
      }
    }