【问题标题】:Adding onClick event to MKAnnotation swift将 onClick 事件添加到 MKAnnotation swift
【发布时间】:2017-04-03 18:20:55
【问题描述】:

我目前正在使用 alamofire 从 Web 请求中获取一些位置,将它们存储在我的 Object 数组中,然后将它们显示在 MKMapView 上。

 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    guard !(annotation is MKUserLocation) else {
        return nil
    }

    let annotationIdentifier = "AnnotationIdentifier"

    var annotationView: MKAnnotationView?
    if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
        annotationView = dequeuedAnnotationView
        annotationView?.annotation = annotation
    }
    else {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
        annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
    }

    if let annotationView = annotationView {
        annotationView.canShowCallout = true
        annotationView.image = UIImage(named: "atm_map_icon")
    }

    return annotationView
}

这是我解析 json 的地方,将结果放入数组中,然后循环遍历数组并将标记添加到地图中

for var i in 0..<json.count{
                let name = json[i]["Name"].string
                let address = json[i]["Address"].string
                let lat = json[i]["Lat"].double
                let lon = json[i]["Lon"].double
                let atm = Atm(name: name!,address: address!,lat: lat!,lon: lon!)
                self.atmArrayList.append(atm)



            }
            print(self.atmArrayList.count)
        }
        for atm in self.atmArrayList{
            let atmPin = AtmAnnotation(title: atm.name!, subtitle: atm.address!, atm: atm, coordinate: CLLocationCoordinate2D(latitude: atm.lat!, longitude: atm.lon!))
            self.annotationArray.append(atmPin)
        }

        self.atmMapView.addAnnotations(self.annotationArray)

我想要实现的是,当用户单击 rightCalloutAccessoryView 按钮时,我想将该注释的特定 Atm 对象传递给另一个 ViewController。

我的猜测是给 Annotations 一个 id,然后从该特定位置的 Atm 数组中获取 Atm??

【问题讨论】:

    标签: ios swift mkmapview mkannotation mkannotationview


    【解决方案1】:

    你需要在你的viewController中实现MapViewDelegate这个方法

    func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
      let anotherViewController = self.storyboard?.instantiateViewController(withIdentifier: "anotherViewController") as! AnotherViewController
      if let atmPin = view.annotation as? AtmAnnotation
      {
        anotherViewController.currentAtmPin = atmPin
      }
      self.navigationController?.pushViewController(anotherViewController, animated: true)
    
    }
    

    希望对你有帮助

    【讨论】:

    • @pavlos 您是否将您的 viewController 设置为您的地图的委托?,self.mapView.delegate = selfand 再试一次必须工作
    • 现在工作了.. 将self.navigationController?.pushViewController(anotherViewController, animated: true) 更改为present(detailedVC, animated: true, completion: nil) 并且工作了.. 谢谢兄弟
    猜你喜欢
    • 1970-01-01
    • 2012-06-16
    • 2012-06-09
    • 2012-10-08
    • 2013-05-04
    • 2021-10-26
    • 1970-01-01
    • 1970-01-01
    • 2011-10-20
    相关资源
    最近更新 更多