【问题标题】:viewForAnnotation not being called (using Alamofire to parse JSON)未调用 viewForAnnotation(使用 Alamofire 解析 JSON)
【发布时间】:2015-07-22 03:34:00
【问题描述】:

我是 iOS 编程新手。我更喜欢使用 Swift。

我要做的是调用一个返回一些 JSON 的 Web 服务,将 JSON 解析为一个名为 Entry 的自定义对象,然后在地图上绘制这些条目。

我正在使用 AlamoFire 进行 Web 服务调用,并使用 SwiftyJSON 将 JSON 解析为如下所示的 Entry 对象

    request(.GET, URLString: "https://myURLThatReturnsJson")
        .responseJSON { (_, _, json, error) in

            if let json: AnyObject = json{

                let jsonEntries = JSON(json)

                for result in jsonEntries["entries"].arrayValue {
                    let business_name = result["business_name"].stringValue
                    let latitude = result["lat"].stringValue
                    let longitude = result["lng"].stringValue
                    let category = result["category"].stringValue
                    let address = result["address"].stringValue
                    let city = result["city"].stringValue
                    let type = result["type"].stringValue
                    let location = result["location"].stringValue
                    let valid = result["valid"].stringValue
                    let comments = result["comments"].stringValue
                    let date = result["date"].stringValue
                    let username = result["username"].stringValue
                    let additional_comment_count = result["additional_comment_count"].stringValue


                    let entry : Entry = Entry(
                        business_name: business_name,
                        latitude: latitude,
                        longitude: longitude,
                        category: category,
                        address: address,
                        city: city,
                        type: type,
                        location: location,
                        valid: valid,
                        comments: comments,
                        date: date,
                        username: username,
                        additional_comment_count: additional_comment_count,
                        title: business_name,
                        subtitle: location,
                        coordinate: CLLocationCoordinate2D(latitude: (latitude as NSString).doubleValue, longitude: (longitude as NSString).doubleValue))

                    entries.append(entry)
                }

                // Now we have our array of entries. Now plot them.

                for entry in entries{

                    self.mapView.addAnnotation(entry)

                }
            }
    }

正如您在上面的代码中看到的那样,我还在 AlamoFire 请求中映射条目(不知道这是否不好,因为我相信 AlamoFire 是在单独的线程上完成的)。

我还有一个 viewForAnnotations 方法,如下所示

    func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
         print("viewForAnnotation called")

         return nil
    }

我的 viewForAnnotation 方法根本没有被调用(打印行上的断点没有被命中)。

感谢任何帮助!

【问题讨论】:

  • 是 mapView.delegate = self 作为部分 viewDidLoad 方法添加还是将 mapView 委托出口连接到 ViewController?
  • 我的 ViewController 有一个类扩展,其定义如下:扩展 MapViewController : MKMapViewDelegate 和 viewForAnnotation 在我的扩展类中定义。
  • 没关系,但是viewcontroller怎么知道它应该处理MKMapViewDelegate方法而不设置mapView的delegate属性呢?
  • 你有一个有效的点。添加 mapView.delegate = self 后,我可以访问 viewForAnnotation 方法!非常感谢。 :-) 如果您想将其写为答案,我会接受。

标签: swift alamofire swifty-json mapkitannotation


【解决方案1】:

请务必将mapview的delegate属性设置为self。

mapview.delegate = self

您也可以通过使用连接检查器(接口生成器)将 mapview 的代理出口连接到 ViewController 来做到这一点

【讨论】:

  • 感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-02
  • 2018-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-23
相关资源
最近更新 更多