Tony Tarng
Tony Tarng

Reputation: 749

Passing data between view controllers through segue

I have a MapViewController with a prepareForSegue(_:sender:)method, which I intend to use to send data to LandmarkTableViewController, and is called when a button is pressed.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let destinationvc = segue.destinationViewController
        if let landmarkvc = destinationvc as? LandmarkTableViewController {
            if let identifier = segue.identifier {

                let library = Landmark(name: "Run Run Shaw Library", properties: ["Chinese Kanji", "Gray", "Green Plants"])

                let bank = Landmark(name: "Hang Seng Bank", properties: ["Chinese Kanji", "Green"])

                switch identifier {
                    case "showLibrary" : landmarkvc.passedLandmark = library // pass data to LandmarkTableViewController
                    case "showBank" : landmarkvc.passedLandmark = bank // pass data to LandmarkTableViewController
                    default : break
                }
            }
        }
    }

The LandmarkTableViewController is properly set up to display the String array properties, with one String on each row. So what I intend to do is pass the appropriate data for the table to properties according to which button was pressed, and let LandmarkTableViewController display the corresponding properties.

class LandmarkTableViewController: UITableViewController {
    var properties = [String]()
    var passedLandmark = Landmark(name: "temp", properties: ["temp"]) // initially set to default value

    override func viewDidLoad() {
        super.viewDidLoad()

        loadSampleProperties()
    }

    func loadSampleProperties() {
        self.properties = passedLandmark!.properties
    }
    // other methods....
}

class Landmark {
    var name: String
    var properties: [String]

    init?(name: String, properties: [String]) {
        self.name = name
        self.properties = properties

        // Initialization should fail if there is no name or if there is no property.
        if name.isEmpty || properties.isEmpty {
            return nil
        }
    }

However, when I run the code, only temp is displayed in the table view. I've been stuck on this for a long time now, so any help is much appreciated!

Edit: loadData() inside of viewDidLoad() is changed to the correct loadSampleProperties(). I made an error while posting the code to the question.

Upvotes: 1

Views: 606

Answers (2)

Valentin Kaltchev
Valentin Kaltchev

Reputation: 54

Code is missing from your quote, so I can't be sure, but I assume your loadData() method is the one that reloads the table view data with Landmark you've passed in prepareForSegue. If that is the case:

viewDidLoad() is called before prepareForSegue, so that all the views and elements of the destinationViewController are loaded and ready to use. Thus, in your case, the table view is loaded with your "temp" data and nothing makes it reload when you set the proper one.

You have two options: You could call loadData()/reloadData() in viewWillAppear for example, which is called after prepareForSegue(). Bare in mind that viewWillAppear will possibly be called again in some other navigation. Otherwise, you could instantiate and present/push the new controller in your parent view controller, instead of using the segue.

Upvotes: 0

Arashk
Arashk

Reputation: 627

I think this should solve your problem if not double check your identifiers and you can make sure to data passing with adding print(passedLandmark) to viewDidLoad() or breakpoint to make sure you getting the data

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)        {
    let destinationvc = segue.destinationViewController
    if let landmarkvc = destinationvc as? LandmarkTableViewController {
        if segue.identifier == "showLibrary" {

            let library = Landmark(name: "Run Run Shaw Library", properties: ["Chinese Kanji", "Gray", "Green Plants"])
              landmarkvc.passedLandmark = library
        }
        if segue.identifier == "showBank" {
            let bank = Landmark(name: "Hang Seng Bank", properties: ["Chinese Kanji", "Green"])
            landmarkvc.passedLandmark = bank
        }
    }
}

Hope this will helps

Upvotes: 0

Related Questions