gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 08:24:46 25 4
gpt4 key购买 nike

我在尝试弄清楚如何通过 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com