我有一个MapView控制器,其中包含注释以及其他数据,例如详细信息和图像信息。我尝试过滤注释及其相关数据,然后在单击标注附件控件中的detailDisclosure按钮时将数据传递给DetailView控制器。
我只能单独传递信息。我可以传递注释标题,我可以将所有注释的数据传递给DetailView控制器。但我的目标是选择注释,单击detailDisclosure按钮,传递所选注释的信息,并在DetailView控制器中查看标题,副标题,详细信息和imageName。
任何见解都将不胜感激。
以下是我一直致力于的代码:
class LocationAnnotation: NSObject, MGLAnnotation {
var customPointAnnotation = CustomPointAnnotation()
var location: Location?
var coordinate: CLLocationCoordinate2D
var title: String?
var subtitle: String?
var imageObj = ""
var details: String?
init(coordinate: CLLocationCoordinate2D, title: String, subtitle: String, imageObj: String, details: String, location: Location? = nil) {
self.coordinate = coordinate
self.title = title
self.subtitle = subtitle
self.imageObj = imageObj
self.details = details
}
}
class MapViewController: UIViewController, MGLMapViewDelegate {
var locs = [Location]()
}
func populateMap() {
// Declare the CustomPointAnnotation and set its coordinates, title, subtitle, image.
let point = CustomPointAnnotation()
for loc in locs {
point.coordinate = CLLocationCoordinate2D(latitude: loc.latitude, longitude: loc.longitude)
point.title = loc.name
point.subtitle = loc.category
point.imageObj = loc.imageName
// Add marker to the map.
mapview.addAnnotation(point)
}
}
func mapView(_ mapView: MGLMapView, annotation: MGLAnnotation, calloutAccessoryControlTapped control: UIControl) {
performSegue(withIdentifier: "moreDetail", sender: annotation.title!)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "moreDetail") {
let controller = segue.destination as! DetailsViewController
controller.location = locs
controller.data = sender as? String
}
}
更新
我能够通过调用CustomPointAnnotation最终传递每个选定位置的数据。以下有帮助:
Swift - Perform Segue from map annotation
func mapView(_ mapView: MGLMapView, annotation view: MGLAnnotation, calloutAccessoryControlTapped control: UIControl) {
performSegue(withIdentifier: "moreDetail", sender: view)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "moreDetail") {
let controller = segue.destination as! DetailsViewController
controller.titleShow = (sender as! CustomPointAnnotation).title
controller.subtitle = (sender as! CustomPointAnnotation).subtitle
controller.imageName = (sender as! CustomPointAnnotation).imageObj
controller.details = (sender as! CustomPointAnnotation).details
}
}