0

I have an app which goes something like this:

first screen loads a collection view, the items that are displayed are kept in a csv file.

you click on one of the items and it loads a new screen, with details from the csv about that item.

I have a csv parse tool, which does a great job of pulling the correct information based on the id of that row in the csv file.

my issue: after the second screen, i have a 3rd screen whcih has a Google Map view, one of the items in the csv file is lat and another lon.

I want to pass the lat and lon values through to the third screen based on the id, so the correct values go through.

I am very new to Swift and I just can't get my head around this one.

1) do I need to parse the CSV file again on the second screen?

2) do I send the details from the already parsed CSV file in the prepare for Segue method? if so can somebody help me code the prepare for segue please

edited to incude code:

first VC prepare for Segue

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "TrackDetailVC" {
        if let detailsVC = segue.destination as? TrackDetailVC {
            if let tr = sender as? Tracks {
                detailsVC.track = tr
            }
        }
    }

first VC CSV Parse code

func parseTrackCSV() {

    let path = Bundle.main.path(forResource: "tracks", ofType: "csv")!

    do {
        let csv = try CSV(contentsOfURL: path)
        let rows = csv.rows

        for row in  rows {
            let trackId = Int(row["id"]!)!
            let name = row["name"]!
            let postcode = row["postcode"]!
            let trackType = row["type"]!
            let locID = row["locID"]!
            let lon = Double(row["long"]!)!
            let lat = Double(row["lat"]!)!
            let tr = Tracks(name: name, trackId: trackId, postcode: postcode, trackType: trackType, locId: locID, lon: lon, lat: lat)

            track.append(tr)

            }
        } catch let err as NSError {
        print(err.debugDescription)

    }

}

so using this how would I write the Prepare for Segue code on the second VC to pass through to the 3rd?

many thanks

Jade Reynolds
  • 201
  • 5
  • 14
  • did you trying with delegate to pass data ? – Nazmul Hasan Feb 24 '17 at 18:14
  • Pass the `latitude` and `longitude` through to your "second screen" via the standard `prepare(for:, sender:)` segue method and then pass the `latitude` and `longitude` via the next segue to your "third screen". If you want anything more than general advice you will need to post code indicating the specific problem you are having. – Robotic Cat Feb 24 '17 at 18:18
  • hi, edited to include the code from first VC to show how it is bringing the data from the first screen to the second. I kind of knew that i need to pass through the same data, but I can't figure out in my own head how. I will ultimately need lat, lon and name passed through – Jade Reynolds Feb 24 '17 at 18:23
  • oh and just to add, the segue to the 3rd screen is via a button – Jade Reynolds Feb 24 '17 at 18:24

1 Answers1

1

Wouldn't be efficient to reparse the CVC.

Your going to need a variable in the third view controller. It's up to you if it's a delegate or the LatLng data.

1 declare an optional for the LatLng in the third view controller and set it in prepare for segue.

2 make a protocol "GoogleMapDelegate" with a function "getLatLng" and have view controller two implement it. Make a optional variable in view controller three for the delegate. Then pass "self" (view controller two) during prepare for segue. Finally when you need to: call yourDelegate.getLatLng()

MarcJames
  • 189
  • 9