Is there any other solutions in iOS for saving the NSDate in one ViewController and showing it in a label in another ViewController rather than NSUserDefaults? I have 2 viewControllers in my project which one of them is DatePicker and another is a normal ViewController with a label.I want to show the time with DatePicker chosen in that label and I use NSUserDefaults.I am curios is there any other solution for this matter?Thanks in advance.
-
1I think this might be the most commonly asked iOS related question. Here's one with a few hundred upvoted answers. :-) http://stackoverflow.com/a/9736559/3708223 – Cooper Buckingham Jun 11 '14 at 14:24
-
I already read those answers but as date is critical the I was thinking maybe there are some other ways to pass those kind of data between pages rather than NSUserDefaults.Thank you – codeGeek Jun 11 '14 at 14:36
-
I think you might need to reread them. You should not be using NSUserDefaults. That would be the hack implementation. :-) Unless you are saving the data between app launches, but even then. NSUserDefaults is intended for User Settings. If you are looking to save data to the device, there are a number of suggested methods out there. – Cooper Buckingham Jun 11 '14 at 15:46
2 Answers
Core Data can be used instead of NSUserDefaults. Core Data is better, though, for larger amounts of data, so for your case, NSUserDefaults is the best option.
Core data is great for relationships and has a nice interface for adding entities and relationships.
It uses key value coding (setValue:ForKey: like NSDictionary) and objects from core data are subclasses of NSManagedObject.
Here is a nice tutorial: http://www.raywenderlich.com/934/core-data-tutorial-for-ios-getting-started

- 713
- 2
- 8
- 19
You can use a singleton class (e.g. DataManager
), and you could keep the selected date here if you have no direct segue between these 2 UIViewControllers
. if you do, just add a property
and set it when you present the controller. However, these won't persist between app launches, so for that you would need the NSUserDefaults

- 7,313
- 2
- 32
- 44