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

标签 swift annotations mapkit

我需要为 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/

相关文章:

ios - 将数据输入到不同类中的不同结构

ios - swift 。 GeneratorOf<T> 生成函数未返回正确的生成器

java - jackson jboss eap6.1.1 注释不起作用

swift - 从屏幕中心获取位置 Swift MapKit

iphone - MapKit:如何平移和缩放以适合我的所有注释?

swift - SceneKit - 运动 + 重力 = 奇怪的运动

ios - 让tableView子View随着tableView滚动

java - @Value 注释类型从 String 转换为 Integer

java - Android 操作栏首页

ios - 在 MKMapView 中使用 UILongPressGestureRecognizer 和可拖动的 MKPinAnnotationView