How should I be transferring data through multiple view controllers?
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?
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.
链接地址: http://www.djcxy.com/p/20694.html上一篇: Java中的数组以及它们如何存储在内存中
下一篇: 我应该如何通过多个视图控制器传输数据?