saving NSDate using NSUserDefaults - ios

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.

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

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

Related

Send data from multiple view controllers

I have been searching how to send data between multiple view controllers but its only seem to be possible to send from one at a time?
I have five view controllers and I want to add text in to the TextFields I have in four of the five of them.
When I am in the last view I want to get the data from the four previous controllers.
You should use the NSUserDefault class.
With the NSUserDefaults class, you can save settings and properties related to application or user data. With NSUserDefaults you can save objects from the following class types:
NSData
NSString
NSNumber
NSDate
NSArray
NSDictionary.
I am 100% sure this link could solve your problem..
http://code.tutsplus.com/tutorials/ios-sdk-working-with-nsuserdefaults--mobile-6039

How to "sync" UI with Core Data?

I would like to know what is the usual approach for syncing UI with Core Data, for example when you display data stored by Core Data in a table view, and you want to show additional info about an object when the user taps its cell in the table view, how would you link the table view cell to a specific object in Core Data ? I have seen many people not recommending to use IDs since Core Data wasn't designed in this way, but then what to use if we are not using an ID ? I would have wanted to do something like : "OK, the selected cell has this ID, let's go in Core Data fetch the object with the same ID" But if I'm not doing this, how can I do ?
Same question for other cases where you need this kind of link... in my case, I need a link between pins on a map and Core Data objects, so that I display additional info when the pin is tapped, a little bit like in Apple Maps for example...
What should I do ?
Thank you.
You can do that by using NSFetchedResultsController - you can subclass your UITableViewController from CoreDataTableViewController (.h .m) and the only method you need to override would be cellForRowAtIndexPath:. Then can access objects directly like this:
MyCustomObject *obj = [self.fetchedResultsController objectAtIndexPath:indexPath];
For your second question, when you are not using a table view there are two options. One you can fetch the object (assuming you are at the very top of the stack) or two, far more common, you have the NSManagedObject instance passed into you using simple dependency injection during the storyboard segue.
Any view that is below/after a table view should never need to fetch or call something like -objectWithID: as the object is already in memory and the controller that is giving you the ID already has the object. Just pass the object around like any other language.

Read/Write a variable from another .swift file

I have two ViewControllers and both with their .swift class file.
The thing is i have a variable declared in the first ViewController, and it will be filled with a value entered by the user in the first view. But then i want to show that value in another view (im using a tab bar controller).
But the problem is i don't know how to reference to that variable from the SecondViewController.swift because the variable was declared in FirstViewController.swift
for the protocol/delegate method of sharing data see:
https://medium.com/swift-programming/ios-swift-protocols-and-delegates-7193f7f58b8a
http://www.raywenderlich.com/75289/swift-tutorial-part-3-tuples-protocols-delegates-table-views
for saving to file using NSCoding, I posted code earlier today here:
SWIFT How to create a NSCoding Subclass and call it from another class?
If you want to save more simply with NSUserDefaults:
Store [String] in NSUserDefaults (Swift)
There are two common ways to move data. If you just want instance data (the current instance of ViewController1) to move to ViewController2 you would pass the data via a segue and the prepareForSegue function. This is one of the most important thongs to learn. I will link to some tutorials below. Keep in mind, Xcode 6 has evolved and the syntax may change slightly. So for the function prepareForSegue, you need to look it up in the documentation by command-click on the word "prepareForSegue" in your swift file and it will take you to the documentation where you can cut an paste.
http://makeapppie.com/2014/07/01/swift-swift-using-segues-and-delegates-in-navigation-controllers-part-1-the-template/
http://bharathnagarajrao.wordpress.com/2014/06/17/swiftly-learning-swift/
The other common way to pass data is to have the data persist by saving to a file. Another set of links. I would also recommend going to github and search for projects with both prepareForSegue and Swift for further examples.

How can i access a ViewController that's a segue from a Tableview in another ViewController from my AppDelegate?

I'm new to CoreData, and my problem is that i want to make an NSManagedObject from a ViewController Class that has a table view, basically i want to save an object inside a tableView "every time the user adds a cell", but that ViewController is presented from a tableview menu, "object at index", the question is, how can i save data for that
ViewController from my AppDelegate? or how can i access that ViewController From my AppDelegate...
Here's an image of my Storyboard and somehow explaining what I'm trying to achieve.
Thanks!!
I've read through your question ten times and i don't seem to be understanding it fully.
From what i understand you want to save the data into core data from the bottom tableviewcontroller which is a UIViewController with an UITableView as a subview.
I don't see how that should be difficult unless i understood the problem wrong.
The basic Xcode template for an iOS project using Core Data implements the initialization within the app delegate but relying on the app delegate as a connector is something i don't do. I always abstract that information into a singleton which can be accessed from any class that implements that logic.
Also the basic Xcode template for a core data project also contains logic for adding entries within the tableview so i would recommend looking at that.
If you want specific example source code i can do that but i recommend looking at the core data template.

Why should I use NSFetchedResultsController in a hierarchical ViewController structure?

I searched a bit but couldn't find the answer I was actually looking for. Simple scenario:
TableViewController with an NSArray of beers. If I select the cell, a detail-view with beer details should be displayed. Now the beers are stored using CoreData.
In the RootViewController I use NSFetchedResultsController to fetch all the beers.
Now my question is: Should I just set the beer property of the destination to the selected beer, or should I create a new NSFetchedResultsController and perform a whole new fetch with a set up NSPredicate?
Where is the difference?
Hmm...
For me I think the biggest factor would be whether the data changes or not.
NSFetchedResultsController is good for performing fetches etc... but it comes into its own when dealing with CoreData entities that change.
The NSFetchedResultsControllerDelegate methods are there to update the table/collection view when the CoreData model is updated on a background thread (i.e. from a network request etc...).
If your model doesn't change at all then I'd go with just passing the beer object in and using the properties of that object.
If the model does change then use the NSFRC and its delegate methods to perform a new fetch.
If you already hold the selected object in your RootViewController and it has all attributes that you will present in the detailViewController, then I see no point setting up the NSFetchedResultController for the detailVC. You can just pass the object to detail in prepareForSegue or so.
The point of using NSFetchedController is that you don't have to update your tableview datasource array and reload rows/table with each change in your list provided you use Apple's CoreDataTableViewController template.

Resources