【问题标题】:Passing data from a map annotation to a new view controller将数据从地图注释传递到新的视图控制器
【发布时间】:2020-06-03 18:00:48
【问题描述】:

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

这是从注释打开的新视图控制器的图片。

这是 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】:

    您可以使用以下代码将数据传递给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);
        }
    }
    

    【讨论】:

    • 感谢您的回答。我已经实现了这一点,当我运行应用程序时它可以工作,但是 placeNameText 没有改变。我将如何解决这个问题?我的代码已更新以匹配我所拥有的。
    • 它确实改变了。在推送视图控制器之前,您必须分配 placeNameText 的值,以便分配一个新的值。
    • 在上面的代码中,让storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 让home = storyBoard.instantiateViewController(withIdentifier: "DetailViewController") 为! DetailViewController home.placeNameText = annotationTitle! // 这里我将数据传递给 detailViewController。
    • 所以,annotationTitle 将被传递给 DetailViewController 并显示在视图中的标签中。
    • 因此,如果您有多个位置具有上述答案中定义的注释,则单击注释时将在其中获得相应的值,并将相同的值发送到 DetailViewController。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多