简体   繁体   中英

How to pass datasource to second View Controller? Swift

So I have a dataModel that pulls Arrays from a plist using my datasource class. In my view controller I call the datasource class with

let dataSource = DataSource()

and then I grab values from the array with

abilities = dataSource.ability[monster.ability! + 2]

My issue is when I grab the Datasource from the second Viewcontroller , there is a 2-4 second delay during the segue transition. How can I pass the datasource over the second viewcontroller , so i can read the values, without having to call let dataSource = DataSource() from within the second view controller?

There are a few different ways to handle passing information from one view controller to another.

However for your scenario I would recommend to create an array property in your second view controller that will hold your Datasource, but only by passing it from your first view controller by calling the prepare(for segue: UIStoryboardSegue, sender: Any?) method.

Here is an example of how I passed simple data to another view controller, pretty much a Master-Detail setup:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let destinationViewController = segue.destination as? DetailNoteViewController

    if segue.identifier == "toNoteDetailView" {
        guard let indexPath = tableView.indexPathForSelectedRow else { return }

        let note = self.notes[indexPath.row]
        destinationViewController?.note = note
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM