I want to store the tapped location of view controller in core data. I am using UITapGestureRecognizer .
As user taps anywhere in the view,i want that location to get saved in core data.
How can i do this? Any idea will be helpful.
Alteratively, rather than a string, you could create an Entity to represent a point with it's x and y values.
Related
I have an array of users
var selectedUsers = [User]()
users are added to this array as they are selected in a UITableView (see screenshot: https://www.dropbox.com/s/uk03mhgi3x4jesy/File%2006-10-2015%2C%2018%2003%2044.png?dl=0)
What I'm having difficulty with is when I press back and then reload the view controller, all the checkmarks disappear.
Is there anyway I can keep track of the selected cells?
Thanks in advance
You could use a delegate to pass a array between the two viewcontrollers. For example, each time you select a row, you can store the userId's associated with the row, in the array. So if you were to press back and then open the view with the UITableView, before you load the UITableView, you can first check if there is an array being passed to the viewcontroller containing the UITableView / if there is an array, check if the count is greater than 0. If the array is not empty, then use a for loop to cycle through the array of users being displayed with the array being passed containing all of the previously selected id's, then add the check marks that match up to the Id's in the array.
Here is a tutorial that possesses a similar example: http://makeapppie.com/2014/08/04/the-swift-swift-tutorial-why-do-we-need-delegates/
The answer to this question is kinda dependent on what you want the application to do... Do you want the User selections to persist even if the app is closed? If so consider using NSUserDefaults or CoreData. Otherwise store the data somewhere that does not get blown away (maybe the root view controller of your application). You could for instance implement the delegate pattern on the view controller with the table view and pass the values to the container view controller.. Just some ideas.
So you can use class NSUserDefaults to save small amount of data. If you want to have persistent store so you have to use Core Data that helps you store you data with scheme you provide.
If you just want to pass data from one view controller to another view controller so you just need use delegation pattern or you can use method prepareForSegue where you can pass some data as well but you can't store this way, just pass.
If you make your question more detailed you will get more explicit answer.
I've been looking around the Internet to find a tutorial or something to explain this.
I need to show some pins on a map. I need to get them from a sqlite database (table with POI's name, lat, lon, and some text). They have to show annotations callouts with title and subtitle and open their detail views.
All that with Objective-C.
How can I do that? Can anybody help and write the code?
I'd recommend using FMDB to get files from a local sqlite db. This stack answer is pretty helpful in getting started on that. It might take an hour to get up to speed on it, but you'll save that hour many times over once you're comfortable with it.
Now that you're able to retrieve data from your db, you need to create MKAnnotations for each pin you want to drop on the map. This tutorial is pretty short and to the point for what you're looking for. Also, don't worry about adding a lot of annotations, MapKit is optimized for have a large number of annotations on a map.
Lastly, you want to be able to go from an annotation to a detail view. MKAnnotationView has a property called leftCalloutAccessoryView where you can add a button. If you just want a quick and dirty fix, set the tag property on the button to the index in the array with the info for the detail view and have every button call the same selector.
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.
I've never used CLLocations so far. What I want is to save a position by clicking on a button into core data. This works. I just start the location update when the button gets clicked, save the position to the database in the delegate method didUpdateLocation and then stop updating again.
What I want now is to show the heading to this position. Since there can be several positions (several table cells) I want to show the heading to each of these positions in the table view cells. Now my problem is that the table view controller contains the LocationManager and is the delegate for it. For the headings the table view cells should be the delegate, but can this work? Or where should I implement the didUpdateHeading delegate method?
Keep the view controller as the delegate. When you get the location/heading update, update your data model used as the table's data source. Then reload the table view rows so the cells can show their updated info based on the updated data model.
You can create a singleton class to handle the location manager changes and store the current location and heading. After that you'll be able to add KVO to this singleton or to send a notification when the location changes. You will also save a lot of battery.
Explanation
My app basically uses a mapview with an overlay of polygons that represent buildings, coupled with an annotation. So for this it imports a custom class called Annotation that handles the popup details when the annotation is tapped, meaning it store the building's name and address. At this time the callout (the blue disclosure button) loads an empty DetailViewController object (as there's not really any data to pass through).
I soon added a new feature in the form of a searchable table that loads custom objects of the Building class (with similar properties to Annotation, plus images and more details) that then loads in the aforementioned DetailViewController class with the building's details.
So to summarise, the MapVC contains multiple annotations, which when the relevant disclosure button is tapped open the DetailVC. SearchVC is accessed by a button on the MapVC and has a table of Building objects, which loads a DetailVC with the relevant data, like so:
Next Step
So now I want to implement functionality into the blue disclosure button on the callout, so when the user taps it it'll load the building details. At the moment all it has is the annotation's details. I could add the extra properties to the annotation to make it complete but I think it's much better to just work with one custom class called Building, which has a MKAnnotation nature. Then this Building class is loaded for the annotations and searchVC's table.
Question
So finally, what's the best way to go around this? I want all the data to be stored independently of any of the VCs in the diagram. I followed a tutorial from Apple (the BirdSighting one) which uses a separate Datacontroller class, which I'd then load into other classes. Is this the best approach?
You're talking about MVC, model-view-controller. This is a very smart way to handle things, and is a good practice to get into. The model stores the data, in this case, building names, etc. The view displays data. The controller is what connects the view to the model, updates the view, gets notifications from users and in turn updates the model.
I use a singleton pattern for model data. Only one instance of a singleton is ever present in an application. That way, the data is not bound to any one particular view controller. Data only gets updated in once place, the model. It's a lot easier to trouble shoot issues with a singular point of convergence for application data objects.
I have a macro that I define in my PCH file.
#import "DataController.h"
#define DATA() [DataController sharedInstance]
In my code I can easily get to my model class by calling
DataController *data = DATA();
All of my views and viewcontrollers access objects stored in DataController, so there are never two view controllers handling separate pieces of information independently. All references point to the same place.
Some people use the AppDelegate object to store data, but it can quickly turn into a 1000 line beast. I prefer to keep the AppDelegate clean :-)