从mapannotation传递数据到一个细节视图控制器SWIFT 3



我有很多麻烦,试图找出我如何能从我的地图视图控制器中的pin地图注释显示我的详细视图控制器。当我单击细节披露按钮时,它将带我到细节视图控制器,但不会传递任何数据。这是我在storyboard中做的基本布局。在注释中,我只有标题、副标题和位置坐标。但在细节视图控制器中,我有超过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!

这是classes "Annotations"

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]
  • 在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"]
      }
    }
    
  • 最新更新