0

Say I have View Controller A, where I have an array with data. Now I also have a couple of other View Controllers (B,C,D, etc...)where I need that array.

I know I could pass the data forward using segues to each of the other View Controllers. But It seems not the perfect solution for me as I have to pass the data each time through a lot of different View Controllers

I would rather have a method where I define in View controller B "Get the data from View Controller A"

(As I understand passing data backwards is not what I want to achieve because I don't want to change the array in View controller B and pass it back to View Controller A. I only want to read/get the data within View Controller B from View Controller A.

Can somebody point me to a solution for this? Or do I have to pass the data forward from VC A to VC B?

I guess it is a matter of taste but I would rather write my code in VC B/C/D in case I need the data from VC A than passing each time data from A to every other VC...

MakiCodes
  • 77
  • 1
  • 10
  • why don't you use a singleton to hold the data instead of holding it in ViewController A? – Nevin Jethmalani Oct 12 '17 at 18:35
  • Thanks! I didn't knew about "singletons", I will research that! – MakiCodes Oct 12 '17 at 18:55
  • Yup the answer below is the way to do it. If you need any other help let us know. It basically allows you to persist data across the entire app – Nevin Jethmalani Oct 12 '17 at 19:21
  • Thanks again, could you look into my comment in the answer below? – MakiCodes Oct 12 '17 at 19:42
  • While you're researching singletons, I suggest you take a look at this: https://cocoacasts.com/are-singletons-bad/ There are cases when singletons can be an appropriate choice but the problem you're trying to solve doesn't appear to be one of them. This topic is a holy war and I'm not trying to start up a big debate here. Just trying to help you learn early on what kind of choice you make if you take the easy way. – Jim Oct 12 '17 at 20:23
  • It sounds you need an actual Model layer. Read up on MVC: https://en.wikipedia.org/wiki/Model–view–controller – GetSwifty Oct 12 '17 at 22:27

2 Answers2

0

Create a singleton class. let's say DataManager.

class DataManager: NSObject {

  // A Singleton instance
  static let shared = DataManager()

  // your array
  var dataArray = [String]()
}

now access this array from anywhere in the app.

Print(DataManager.shared.dataArray)
rushisangani
  • 3,195
  • 2
  • 14
  • 18
  • Hmmm... I already created my Array in my "View Controller Class" quite complicated, how can I pass this final array into my DataManger Class, so that it gets global? – MakiCodes Oct 12 '17 at 19:36
  • Can you be a little more specific? If you can post some code about the array that you are using (the type etc.) that would be helpful. If you are using the above class then you should do it like this DataManager.shared.dataArray = yourCurrentArray – Nevin Jethmalani Oct 12 '17 at 19:44
  • Thank you, I got it and it works! But I think I need a break for today :) – MakiCodes Oct 12 '17 at 20:56
0

Do I have to pass the data forward from VC A to VC B?

You don't have to, but you should.

The suggested singleton pattern is considered an anti-pattern nowadays, you can read more about why here: What is so bad about singletons?.

It's not just a matter of taste, your code will be more maintainable if you avoid singletons.

Even if you use CoreData instead of an array in ViewController A, you will/should pass references from A to B, be it the Managed Object Context or an array of objects that you retrieved from CoreData in ViewController A. You can see this happening in the default template provided by XCode/Apple for a MasterDetail app with CoreData (just create a new project for that).

Arnaud
  • 17,268
  • 9
  • 65
  • 83