gpt4 book ai didi

swift - 为 MapKit 中的每个注解添加 Id

转载 作者:搜寻专家 更新时间:2023-10-31 08:30:20 25 4
gpt4 key购买 nike

我需要为 map 中的每个注释添加一个 Id 以通过单击注释中的信息按钮打开新的 View Controller ,我的问题是准备和执行 segue,我创建了它,但我不知道如何通过id 到 segue。

我为注释创建了子类来存储来自 JSON 的 ID

那是我的部分代码:

从 JSON 获取所有数据:

for location in self.locations {

let annotation:MyAnnotation = MyAnnotation()
annotation.title = location["truck_name"] as? String
annotation.customTruckId = location["truck_id"] as? String
annotation.coordinate = CLLocationCoordinate2D(latitude: (location["coor_x"] as! NSString).doubleValue, longitude: (location["coor_y"] as! NSString).doubleValue)
self.AllMapView.addAnnotation(annotation)

}

我创建的子类:

class MyAnnotation : MKPointAnnotation {
var customTruckId : String?
}

问题出在这里:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "ShowTruckFromMap") {

let des_VC = segue.destination as! TruckDetailsVC

des_VC.truck_id = "How to get Id here !"
}
}


func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
if control == view.rightCalloutAccessoryView {

performSegue(withIdentifier: "ShowTruckFromMap", sender: self)
}
}

谢谢

最佳答案

您可以在不使用 Segue 的情况下显示您的 UIViewController。您必须首先将标识符放入您的 UIViewController 中:identity Inspector -> Identity -> Storyboard ID = TruckDetailsVCIdentifier

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
if control == view.rightCalloutAccessoryView {
let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let desController = mainStoryboard.instantiateViewController(withIdentifier: "TruckDetailsVCIdentifier") as! TruckDetailsVC
let TruckAnnotation = view.annotation as? MyAnnotation
desController.truck_id = (TruckAnnotation?.customTruckId!)!
show(desController, sender: self)

}
}

关于swift - 为 MapKit 中的每个注解添加 Id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44796187/

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