将数据通过segue从map callout中的detailDisclosure按钮传递到新的DetailView

时间:2017-06-30 05:01:31

标签: ios swift mapkit segue

我使用JSON文件在MapView上填充了几个引脚。这些引脚中的每一个都正确显示带有标题,副标题,图像和detailDisclosure按钮的标注。

我正在尝试在MapView和详细视图(排列为TableView)之间创建一个segue,以便当用户单击detailDisclosure按钮时,它们将被带到Detail View屏幕。

我创建的segue完美无缺,然而,我无法弄清楚如何传递数据。如何通过此segue成功传递数据,以使其显示在详细信息视图中?请参阅下面的相关代码。

我的segue代码:

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    self.performSegue(withIdentifier: "toShowLocationDetail", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "toShowLocationDetail" {
        // I DON'T KNOW WHAT TO PUT IN HERE - I THINK THIS IS WHERE THE INFORMATION ABOUT THE DATA GOES
    }
}

我不确定你是否需要这个,但这是我的ViewDidLoad方法(我用它来解析JSON文件并填充注释:

var locations = [Location]()

override func viewDidLoad() {
    super.viewDidLoad()

    // parse json
    if let locationJson = readLocation(){
        if let locationArray = locationJson["locations"] as? [[String:Any]]{
            for location in locationArray{
                locations.append(Location.init(locationInfo: location))
            }
            print(locations.count)
        }
    }
    // end parse json

    nearMeMap.delegate = self

    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.requestWhenInUseAuthorization()
    self.locationManager.startUpdatingLocation()
    self.nearMeMap.showsUserLocation = true


    // Show annotation
    for location in locations {
        let annotation = MKPointAnnotation()
        annotation.title = location.name
        annotation.subtitle = location.type
        annotation.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
        self.nearMeMap.addAnnotation(annotation)

    }

}

重要的是要注意我已经在TableView和DetailView之间有一个功能segue。我现在想要允许用户通过MapView访问相同的DetailView页面。

DetailView中设置的变量(当前使其能够显示TableView中的数据)是:

var location:Location!

编辑: 这是Location.swift类:

class Location: NSObject {
    var id: String = ""
    var name: String = ""
    var type: String = ""
    var location: String = ""
    var image: String = ""
    var activity: String = ""
    var isVisited: Bool = false
    var rating: String = ""
    var latitude: Double = 0.0
    var longitude: Double = 0.0

    init(locationInfo:[String:Any]) {
        self.id = locationInfo["id"] as! String
        self.name = locationInfo["name"] as! String
        self.type = locationInfo["type"] as! String
        self.location = locationInfo["location"] as! String
        self.image = locationInfo["image"] as! String
        self.activity = locationInfo["activity"] as! String
        self.isVisited = locationInfo["isVisited"] as! Bool
        self.latitude = locationInfo["latitude"] as! Double
        self.longitude = locationInfo["longitude"] as! Double
    }

    public var coordinate: CLLocationCoordinate2D { get {
        let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
        return coordinate
        }
    }

}

1 个答案:

答案 0 :(得分:2)

您必须跟踪所选的Annotation。然后,您可以使用该批注的coordinate属性通过prepare(for:sender:)方法传递给DetailView。

var selectedAnnotation: MKPointAnnotation?

func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
    self.selectedAnnotation = view.annotation as? MKPointAnnotation
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let filteredLocations = locations.filter { (location) -> Bool in
        return (location.latitude == self.selectedAnnotation?.coordinate.latitude && location.longitude == self.selectedAnnotation?.coordinate.longitude)
    }
    let selectedLocation = filteredLocations.first
    if segue.identifier == "toShowLocationDetail" {
        let destinationViewController = segue.destination as! DetailView
        destinationViewController.location = selectedLocation
    }
}

在您的DetailView课程中,将location属性设为可选,例如:var location: Location?