将数据从地图注释传递到新的视图控制器

问题描述 投票:0回答:1

[目前,我有一个地图视图,可显示地图上的位置。在地图上选择位置注释后,它将打开一个新的视图控制器。如何将显示在地图上的注释中显示的地点名称传递给新的视图控制器?

这里是从注解打开的新视图控制器的图片。enter image description here

这是mapView的代码。

import UIKit
import MapKit

struct PlacesOnMap {
var name: String
var latitude: Double
var longitude: Double

init(name: String, latitude: Double, longitude: Double) {
    self.name = name
    self.latitude = latitude
    self.longitude = longitude
}
}

class MapViewController: UIViewController {

@IBOutlet var mapView: MKMapView!

var places = [PlacesOnMap(name: place 1), 
PlacesOnMap(name: place 2)
]

override func viewDidLoad() {
    super.viewDidLoad()

setupAnnotations()
}

func setupAnnotations() {
    let places = places.map { placeOnMap -> MKPointAnnotation in
        let place = MKPointAnnotation()
        place.coordinate =  CLLocationCoordinate2D(latitude: placeOnMap.latitude, longitude: placeOnMap.longitude)
        place.title = placeOnMap.name
        return place
    }
    mapView.addAnnotations(places)
}

extension MapViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    guard let annotationTitle = view.annotation?.title else { return }
    let storyboard: UIStoryboard = UIStoryboard(name: "MyCommunityStoryboard", bundle: nil)
    let destVC = storyboard.instantiateViewController(withIdentifier: "DetailMapItemsViewController") as! DetailMapItemsViewController
    destVC.placeNameText = annotationTitle!
    navigationController?.pushViewController(destVC, animated: true)
}
}

这是到目前为止我的DetailViewController拥有的。

import UIKit

struct PlacesDetailView {
var name: String

init(name: String) {
self.name = name
}

class DetailMapItemsViewController: UIViewController {

@IBOutlet weak var nameOfPlaceLabel: UILabel!

var placeNameText: String = ""

override func viewDidLoad() {
    super.viewDidLoad()

    nameOfPlaceLabel?.text = placeNameText
}
}
ios swift xcode mkmapview
1个回答
1
投票

您可以使用以下代码将数据传递到DetailViewController

import UIKit
import MapKit

struct PlacesOnMap {
    var name: String
    var latitude: Double
    var longitude: Double

init(name: String, latitude: Double, longitude: Double) {
    self.name = name
    self.latitude = latitude
    self.longitude = longitude
    }
}

class ViewController: UIViewController, MKMapViewDelegate {

    @IBOutlet weak var mapView1: MKMapView!
    var places1 = [ PlacesOnMap(name: "Bangalore", latitude: 12.9716, longitude: 77.5946), PlacesOnMap(name: "Mysore", latitude: 12.2958, longitude: 76.6394)]

    override func viewDidLoad() {
        super.viewDidLoad()
        setupAnnotations()
        mapView1.delegate = self
}

func setupAnnotations() {
    let places = places1.map { placeOnMap -> MKPointAnnotation in
        let place = MKPointAnnotation()
        place.coordinate =  CLLocationCoordinate2D(latitude: placeOnMap.latitude, longitude: placeOnMap.longitude)
        place.title = placeOnMap.name
        return place
    }
    mapView1.addAnnotations(places)
}
    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
         guard let annotationTitle = view.annotation?.title else
               {
                   print("Unable to retrieve details")
                return
               }
      print("User tapped on annotation with title: \(annotationTitle!)")

        let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let home = storyBoard.instantiateViewController(withIdentifier: "DetailViewController") as! DetailViewController
         home.placeNameText = annotationTitle!
        navigationController?.pushViewController(home, animated: true);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.