I keep hearing two different sides to this story. I hear nsuserdefaults is the best way to pass data from one controller to another while using property is the best way. I got a bunch of answers on stackoverflow and now wondering what you guys think.
-
2Your question is too general IMHO. Valid options are: properties, user defaults, shared model, database, files on the filesystem..etc Depends on the problem at hand. – iska Aug 27 '13 at 19:46
-
3NSUserDefaults to pass data around? What... – Mick MacCallum Aug 27 '13 at 19:49
-
6Whoever told you `NSUserDefaults` was the best way is wrong. – Carl Veazey Aug 27 '13 at 19:53
3 Answers
There is no best way to pass data, it really depends on the issue you are trying to solve. That being said dependency injection
is the most common one, that is simply instantiating your view controller and setting the properties you need.
As a note NSUserDefaults most definitely should NOT be used to pass data between view controllers, it is meant to be used as a way to persist state between application launches. Maybe you are referring to a singleton object?.

- 18,436
- 13
- 85
- 118
-
I wouldnt quite call that dependency injection. I think depedency injeciton usually implies substituting a piece of functionality that could still be hardcoded, but is made more flexible with injection. Passing data between view controllers through properties or an init isnt quite the same thing. http://en.wikipedia.org/wiki/Dependency_injection – Justin Meiners Aug 27 '13 at 19:55
-
@JustinMeiners In a simplified sense that is what is happening when you set the properties to different values. – Oscar Gomez Aug 27 '13 at 20:00
-
1I think they are fundamentally different concepts and ideals even if if the syntax is the same. – Justin Meiners Aug 27 '13 at 20:02
While you certainly can use NSUserDefaults
for passing information around your application, I find that it is often cleaner and more obvious to pass information explicitly whenever possible.
For example, if you have a view controller with a list of book titles and another view controller that gives you details about a selected book, this would be a straightforward way to pass information from one to the other:
BookDetailsViewController *bookDetailsViewController = [[BookDetailsViewController alloc] init...];
bookDetailsViewController.book = selectedBook;
One view controller has information, the other needs it, so the information is simply passed from where it is to where it needs to go.
If information has to go back the other way, from a presented view controller to its presenter, blocks or delegation can be utilized in a similarly straightforward way.
If information needs to be saved or used throughout an app, this is where NSUserDefaults
and/or singletons can be useful. In general though, I would not put information in a place where it is not actually needed or used.

- 2,077
- 17
- 16
-
So would nsuserdefaults be good to use say if the user has to go through multiple view controllers and needs to later be used for an email? – Two Face Aug 27 '13 at 19:56
-
@TwoFace no it is usually for persistant storage between sessions. (Save which page the user was on or their username etc) – Justin Meiners Aug 27 '13 at 19:57
-
2NO! NSUserDefaults is not made for this. Do NOT use it for this. – Mick MacCallum Aug 27 '13 at 19:58
-
But in my case it has to stay in a certain view controller. For example, if I push a button in vc1 it saves text in vc2 and if I go three view controllers in the App (vc5) then decide to go back to vc2 the data needs to be there. – Two Face Aug 27 '13 at 20:01
-
Again, it really depends on your architecture. If you're using navigation controllers, the data should be in tact when you navigate back (barring memory issues). Tab controllers, modals, etc. all have their own ways of being presented and/or kept in memory, so how you save the state of your application depends on this sort of thing. It sounds like looking more into the use of singletons may be beneficial. I know a lot of people have differing views on how singletons should be used or even if they should be used, but done correctly, they can make this kind of task much easier. – Eric Aug 27 '13 at 21:02
When I need to access the same data from different places in the application, I use a shared model object and use it as a singleton.
Every object that needs it holds a weak reference to it, and the object exposes an API to query its properties.
If you also need to persist the data in this shared model, make it implement the NSCoding
protocol and add methods to serialize it to a file.

- 11,952
- 7
- 37
- 63