Swift:从for循环中保存JSON变量

时间:2017-01-31 12:23:43

标签: ios swift mapbox

我遇到了以下问题:我正在调用JSON文件,将其解析为数据字符串,创建MapBox注释点。一切顺利。 我无法弄清楚的是如何为当前点击的点的注释数据(例如标题)保存变量。

我创建了一个由stationNameString填充的变量point.title。问题是,因为它在for循环中,所以segued窗口中的所有结果destViewCont.LabelText显示JSON系列标题中的最后一个(逻辑上,因为循环已停止),但每个{{1是不同的(这是正确的)。由于循环已停止并且我在代码中更进一步,我无法获取正在点击的点的正确标题。

如何从某个注释中获取要传递给segued模态的标题?

addAnnotation(point)

编辑:添加了子类

class MapController: UIViewController, MGLMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet var mapView: MGLMapView!

var stationURL = "http://..."

var stationNameString = String()

override func viewDidLoad() {

    super.viewDidLoad()

    let styleURL = NSURL(string: "...")
    let mapView = MGLMapView(frame: view.bounds, styleURL: styleURL as URL?)
    mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

    mapView.delegate = self

    // Set the map’s center coordinate and zoom level.
    mapView.setCenter(CLLocationCoordinate2D(latitude: 0, longitude: 0), zoomLevel: 9, animated: true)
    view.addSubview(mapView)

    mapView.userTrackingMode = .follow

    Alamofire.request(stationURL).responseJSON { response in

        if response.result.value != nil {
            NSLog("Success")
            let stationJSON = JSON(response.result.value!)

            let allStations = stationJSON["Stations"]

            for (_, object) in allStations {
                let stationId = object["id"].stringValue
                let stationName = object["name"].stringValue
                let stationLat = object["lat"].stringValue
                let stationLng = object["lng"].stringValue
                let stationStreet = object["street"].stringValue
                let stationCity = object["city"].stringValue
                let stationPostcode = object["postcode"].stringValue

                if stationLat != "0.000000" && stationLat != "1.000000" {
                    let point = SubMGLAnnotation(
                        coordinate: CLLocationCoordinate2D(latitude: Double(stationLat!)!, longitude: Double(stationLng!)!),
                        title: stationName! + " (" + stationId! + ")",
                        subtitle: stationStreet! + ", " + stationCity! + " " + stationPostcode!
                    )

                    pointAnnotations.append(point)

                }

            }
            mapView.addAnnotations(pointAnnotations)
        }
    }

}



func mapView(_ mapView: MGLMapView, rightCalloutAccessoryViewFor annotation: MGLAnnotation) -> UIView? {

    return UIButton(type: .detailDisclosure)

}

func mapView(_ mapView: MGLMapView, annotation: MGLAnnotation, calloutAccessoryControlTapped control: UIControl) {

    performSegue(withIdentifier: "toDetail", sender: view)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    let destViewCont : StationController = segue.destination as! StationController

    destViewCont.LabelText = stationNameString

    if (segue.identifier == "toDetail" ) {
        print("Segue toDetail")

    }

}

编辑:更改了对segue的准备,但这会导致应用程序崩溃。

class SubMGLAnnotation: NSObject, MGLAnnotation {

var coordinate: CLLocationCoordinate2D
var title: String?
var subtitle: String?

var image: UIImage?
var reuseIdentifier: String?

init(coordinate: CLLocationCoordinate2D, title: String?, subtitle: String?) {
    self.coordinate = coordinate
    self.title = title
    self.subtitle = subtitle
}
}

无法将“MGLMapView”(0x1006a7000)类型的值转换为“App.SubMGLAnnotation”

0 个答案:

没有答案