我需要在地图中为每个注释添加一个Id以通过单击注释中的info按钮打开新的视图控制器,我的问题是准备并执行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?
}
问题在于segue:
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)
}
}
由于
答案 0 :(得分:3)
您可以在不使用Segue的情况下显示您的UIViewController。 您必须首先在UIViewController中添加一个标识符: 身份检查员 - >身份 - >故事板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)
}
}
答案 1 :(得分:1)
我找到答案:
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: "TruckDetailsVC") as! TruckDetailsVC
let TruckAnnotation = view.annotation as? MyAnnotation
desController.truck_id = (TruckAnnotation?.customTruckId!)!
show(desController, sender: self)
}
}
全部谢谢