首页 文章

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

提问于
浏览
1

我'm having heaps of trouble trying to figure out how I can present my detail view controller from a pin map annotation in my map view controller. When I click the detail disclosure button, it will take me to the detail view controller, but no data will be passed. It'只是我在故事板中制作的基本布局 . 在注释中,I only have the a title, subtitle, and location coordinates . 但在我的详细视图控制器中,我有超过15种不同的数据 .

我如何呈现它,以便当我单击 Map 注释时,它将带我到其各自的详细控制器与所有数据,而不仅仅是来自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

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

相关问题