0

I have been playing around with a lot of stuff involving arrays and scrollviews. I have mostly stayed within the confines of view controllers, so usually i'll grab data from firebase, add it to an array, and then send it to the tableview or collectionview. What I'm trying to do now is actually navigate between viewcontrollers (or multiple copies of the same view controller) and applying the array items to each view controller.

For example I want to be able to grab some photos from firebase, put them in an array of url strings or whatever. Then I want to put a photo on the background of a view controller. Then when I push the over button it goes navigates to the next view controller and puts the next photo as the background there, etc.

I understand there are probably multiple ways to do this and I was wondering what is the most efficient way? Do I just put an array in a Global class and access that from all the view controllers? Or do I just keep the array in the first view controller, then as I navigate, keep sending it to the next view controller over and over? Also there will be a LOT of items and objects and arrays here so that's why I'm looking for efficiency. Thanks in advance to anyone able to help with this, and I hope I explained it well enough!

Wayne Filkins
  • 494
  • 6
  • 20
  • Possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Kekoa Nov 28 '16 at 05:57
  • I already knew how to pass data from one vc to another. See 3rd paragraph. I want the most efficient way, and for the data to already be accessible there. There is a reason for this but it isn't relevant so I didn't mention it. – Wayne Filkins Nov 28 '16 at 21:04

1 Answers1

2

This is a very simple way of adding and retrieving String value from a struct, here you are saving the image url string as a value in a dictionary and it's key is going to be the ViewController name.

struct SavedData {
    static private var imagesDictionary: [String: String] = [:]

    static func image(for viewController: UIViewController) -> String? {
        return imagesDictionary["\(type(of: viewController))"]
    }

    static func add(image name: String, for viewController: UIViewController) {
        self.imagesDictionary["\(type(of: viewController))"] = name
    }
}

saving a value is very simple, if you're saving the data in a viewController and you want a specific image to be saved for that viewController you can use self

SavedData.add(image: "img1.png", for: self)

And if you want to save an image for a different viewController, do it like this.

SavedData.add(image: "img2.png", for: SecondViewController())

Retrieving the image is also very simple, you should call this method in the viewController that you want to assign the image to.

let savedImage = SavedData.image(for: self)
print(savedImage!)
J. Koush
  • 1,028
  • 1
  • 11
  • 17
  • Ok I think i'll have to do this but with some sort of loop or something, to get the next value from the array for each view controller. I think it should work – Wayne Filkins Nov 26 '16 at 19:36