1

What is the best way to pass data from one view controller to another?

Suppose I push another view controller onto the present view controller. If I have to give some data to the view being pushed from the present view controller how do I do it?

Also how do I pass data from the pushed view to the earlier view?

So far I have done this by declaring functions in each controller and calling them. I find it dirty and want a cleaner way to do this. I don't want to use plists or sqlite as the data isn't that significant.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
HG's
  • 818
  • 6
  • 22

2 Answers2

3

Keeping the data in AppDelegate is not a good idea if the amount of data is quite large since the data will live throughout the application lifecycle.

Why not call the setter(s) of the newly created UIViewController object to pass the data so that you can release it if the view goes offscreen or gets deallocated.

Penang
  • 1,305
  • 13
  • 18
  • 1
    I'm confused as to why calling a property setter was not an immediately given answer. – GendoIkari Mar 11 '11 at 13:55
  • I have already used the property setter. But it gives errors sometimes and is not a good way to set variables. – HG's Mar 11 '11 at 13:59
  • What errors? And why do you say it is not a good way? Setters are specifically created in the framework to set variables. – GendoIkari Mar 11 '11 at 14:02
  • Because for passing the variables, I need to declare them as a property for the respective classes. This leads to a lot of variables being declared. Also, as I said, the data required is temporary and shouldn't be actually declared as properties. The code gets really dirty. On top of that, I have to do memory management for each declared variable. – HG's Mar 11 '11 at 14:09
  • 2
    Why not create a "Model" object that contains all the variables. If done correctly, simply releasing this object will result in releasing the variables it contains. – Penang Mar 11 '11 at 16:57
0

Why can,t you try making the variable in AppDelegate.Give that data to the variable of the AppDelegate and fetch it wherever you want in your application or UIViewControllers you will get that data.That value will be assigned to the the variable of the app delegate upto the termination of the application.

Try taking help from already asked several times Pass Data Between Several Viewcontrollers in a App.

Hope you get my point .....Good luck!

Community
  • 1
  • 1
Sudhanshu
  • 3,950
  • 1
  • 19
  • 18