1

I have three views managed by tab bar controller and I would like to have shared data between the three (I have a separate login screen that passes data to the first view but my second view needs to utilize some of that data). What is the best way to share data between the different views?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
CoderNinja
  • 571
  • 3
  • 7
  • 20
  • you should consider global variable in this case. – Pawan Rai Feb 09 '14 at 08:14
  • @pawan whats the right way of doing global var. i defined a static nsdictionary in the header file of my first view and included the .h file in the second view but for some reason the data doesnt carry over – CoderNinja Feb 09 '14 at 16:43
  • you have many options like NSUserDefaults, AppDelegate, global classes etc. check this [global variable](http://stackoverflow.com/questions/6065965/how-to-define-a-global-variable-that-can-be-accessed-anywhere-in-my-application) – Pawan Rai Feb 09 '14 at 17:14
  • NSUserDefaults seemed to work well – CoderNinja Feb 10 '14 at 02:09

1 Answers1

1

Your question is too general. There are many ways to solve this problem and how you do it depends on what you are trying to achieve. For example, you might create an object to hold this data and set it as a property on each controller. You also might use NSNotifications to let controllers know of a system event and place relevant data in the userInfo dictionary. Other designs involve the use of Singletons or delegates.

So you need to really get a clear idea of what you want, code that up and if/when you have a problem with it, come back to us them. Alternatively if you questions are more about design, you might want to consider posting on Programmers.

drekka
  • 20,957
  • 14
  • 79
  • 135
  • Well specifically I have a nsdictionary in the firs view controller that has a list of school classes and I want to display those courses in the second view controller. However, I dont know what would be the best approach of accessing that data in the second view controller. – CoderNinja Feb 09 '14 at 14:30
  • @drekka I wonder if you wouldn't mind elaborating slightly on how to create an object that can be a property of multiple controllers. – emersonthis Aug 09 '14 at 17:38