Swift:如何将MAP PIN数据传递给新的VC



我正在为将数据从地图引脚传递到新的VC。

方案

MAP PIN数据(名称,字幕& coordinates)当前正在JSON文件中读取。删除地图引脚,单击地图引脚时,新视图将打开并在新VC中的标签中显示相应的标题和字幕以及其他一些数据。

我一直在关注本教程,以从JSON文件中提取数据。

问题:我正在努力寻找一种将数据从地图引脚传递给标签的方法。

我已经完成了一个以前的项目,在该项目中,我将数据从一个Textfield传递给标签。

非常感谢!

编辑:

我意识到我没有提供任何代码。

newfeaturevc

// Callout accessory
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let identifier = "POIAnnotations"
    if annotation is POIAnnotations {
        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
        if annotationView == nil {
            annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            annotationView!.canShowCallout = true
            let button = UIButton(type: .detailDisclosure)
            annotationView!.rightCalloutAccessoryView = button
        }
        else {
            annotationView!.annotation = annotation
        }
        return annotationView
    }
    return nil
}
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    if control == view.rightCalloutAccessoryView {
        performSegue(withIdentifier: "showAnnotationInfo", sender: self)
    }
}
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showAnnotationInfo" {
        let destViewController = segue.destination as! ShopDetailViewController
    }
}

poiannotations

此类是为名称,字幕和坐标存储的变量。

class POIAnnotations: NSObject, MKAnnotation {

var title: String?
var subtitle: String?
var coordinate: CLLocationCoordinate2D
init(title:String, subtitle:String, coordinate:CLLocationCoordinate2D) {
    self.title = title
    self.subtitle = subtitle
    self.coordinate = coordinate
    super.init()
}
class func fromJSON(_ json: [JSONValue]) -> POIAnnotations? {
    // 1
    var title: String
    if let titleOrNil = json[1].string {
        title = titleOrNil
    } else {
        title = ""
    }
    var subtitle: String
    if let subtitleOrNil = json[2].string {
        subtitle = subtitleOrNil
    } else {
        subtitle = ""
    }
    // 2
    let latitude = (json[3].string! as NSString).doubleValue
    let longitude = (json[4].string! as NSString).doubleValue
    let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
    // 3
    return POIAnnotations(title: title, subtitle: subtitle, coordinate: coordinate)
}

}

secondvc

class ShopDetailViewController: UIViewController {

@IBOutlet weak var shopName: UILabel!
@IBOutlet weak var shopRating: UILabel!
@IBOutlet weak var cancelButton: UIBarButtonItem!
var shopNameData = "name"
var shopRatingData = "rating"

我已经尝试使用的方法与我从poiannotations类中的JSON文件中获取数据相同。

我尝试调用fromjson函数,然后尝试将其存储在变量中以显示在标签上。但是我无法解决如何做。

使用mkmapviewdelegate方法检测敲击哪个引脚,然后在View Controller中显示其数据。

func mapView(MKMapView, didSelect: MKAnnotationView)

最新更新