1

Passing data between view controllers in iOS development seems overly complicated. Here is what I've come to realize: Passing data forward (pushing a view) or directly segueing to a view can be easily done with the -(void)prepareForSegue method. Data can easily shuffle back and forth between view controllers using the presentingViewCotnroller and presendedViewController properties. Here is an example of data going from the second view controller back to the first view controller. Here is an example code of what I'm talking about:

    ((FirstViewController *)self.presentingViewController).firstViewControllerProperty = self.secondViewControllerProperty;

However I'm stuck when trying to pass data through multiple view controllers. Everyone seems to say use delegation and think in terms of MVC. When I attempt to do this I get stuck. Here is why: If viewControllerA tells the model to hold some data and then viewControllerD needs the data I end up having viewControllerD reference an instance of the model. (I do this b/c I don't how else to access the model from a view controller) I'm assuming when I create an instance of the model, that mean I have a fresh model without the data I was trying to access.

Then I realize that maybe I should be setting up my model as a singleton. I'm assuming singletons are when you set up one class that is referenced by the all other view controllers without them having to create instances of the model class. The more I read about singletons the more I feel like I'm getting off track. I try putting all these things together and it gets overly complicated.

Am I missing something here? Should I be able to reference my model from various view controllers without creating new instances of it? I'm relatively new to iOS programming, and I'm looking for somebody to point me in the right direction. Should I be putting my energy into learning about delegation? or into learning about singletons? or into learning about target actions? Is there an easy tutorial / book recommendation to learn about transferring data among view controllers?

iOSAppGuy
  • 633
  • 7
  • 23
  • This has been answered in detail here: http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – marrock Aug 09 '14 at 01:32

1 Answers1

0

If you want the easiest way, simply add a property to a view controller and when you want to pass data to that view controller, just set the property to the data you are trying to pass. Another way is to create a method on your view controller to accept data, and then from the calling code simply call that method on that view controller with the data you want to send.

As long as you have a reference to the controller you want to pass the data to, you can set properties or call methods as often as needed.

Don't get too confused with delegates, all they are is a way to provide an interface for communication using methods. I would say it is essential to understand delegates in iOS development, since they are used so heavily.

Read up on Objective-C protocols, which are similar to interfaces in other languages. Delegates are simply what we call objects who implement a protocol.

Only pass data that is necessary to know from the previous view controller. You don't need to pass your data store each time you push a view controller, just push an id of the object you're working on or any variables that the new view will need to do its job. Your view controllers should all be able to access the data store independently of each other, don't pass around data that is easily fetched from the data store.

Kekoa
  • 27,892
  • 14
  • 72
  • 91
  • When I was building a black jack card counting app, I would have the a "mainView", a "settingsView" and a "cardView". When I would select options in my settingsView I would set my number of decks property, the min and max bet property, and so on. the "settingsView" would create those properties and then my other views would have properties that would store those variables (thus no data would be lost). Are you suggesting my settingView would call a "mainView" method. That method would set the data I need on my main view. But wouldn't that data be cleared going to my card view? – iOSAppGuy Feb 09 '13 at 00:29
  • I think your question is making more sense now. I think what you're missing is a model, if you're thinking in terms of MVC. There's too much chatter going on between views, which I think is your complaint. You might consider having a persistent data store(such as NSUserDefaults, Core Data, etc.) that settings would save to. Then simply tell the necessary views that "the settings have changed" by calling a method and they would pull the data from your data store themselves. These views need not be so dependent on each other for data they can easily get themselves. – Kekoa Feb 09 '13 at 00:37
  • That's exactly what I feel I need. I've looked into Core Data and realize that is a bit over my head at this point. I feel like Core Data is an overly complex way of storing my setting but it would work. When I start diving into Core Data, I realize I it's a bit over my head. I then start look into delegation, singletons, etc. I'm like - really? All this because I want my black jack app to remember that the min bet is always $5? Seems like a lot. Knowing this, what route would you suggest I learn first? Maybe start with NSUserDefaults? Is that simple enough to wrap my head around? – iOSAppGuy Feb 09 '13 at 01:09