I have an app that fetches web data and stores it in CD-db. So far, FirstViewController (MKMapView) fetches CD-db into NSMutableArray and uses its values to plot locations by getting the array of custom objects from the db. I cycle thru a for loop while creating the annotation (pin). Each pin gets its distance property set from a calculation I use for the distance from userLocation to each pin.
This app runs in a tabbarcontroller and the other tab is a tableview listing of these locations. I get the values for that tableview by fetching the CD-db and getting the array's values at cellForRowAtIndexPath method and putting the values into the cell.textLabel etc...
My question is, since the distances from userLocation to each pin are calculated on the mapview controller, how do I get those distances on the tableview controller?
You could set the mapview controller as the tableview's datasource. That way only one viewcontroller fetches data. Once you build the mapview controller's array of custom objects it can be used to respond to all the tableview datasource calls such as cellForRowAtIndexpath and numberOfRowsInSection
Related
Im developing a golf score recording app with three view controllers.
The first view controller is a table view controller that has each round that you play via an add round button. When you click a golf round(cell) it takes you to a new view controller that has 18 buttons correlating to each hole of that round. When you click on a hole button (i.e. Hole 1), it then pushes you to a new 3rd view controller where you can record values such as the Par of that hole, the yardage, and then also text fields to input each players score(all this being stored via core data).
Here is my dilemma. Im using core data to persist all the data. Im curious on what the best way to populate text labels on my 3rd view controller with information from my first view controller. Right now I am using the prepareForSegue function to create instances of the new view controllers and then setting the properties of the new view controllers via the prepareForSegue function.
This works just dandy. however, when i get to the third View controller i want to populate some of my fields with some of the data that corresponds to that round. And because the third VC is not a table view I'm lost at how to make sure its fetching the correct data from the correct corresponding indexPath. Right now I'm just passing values from the tableview through properties in the 2nd VC, and then passing those properties again to the 3rd VC. Is there a way i can just do a fetch request on a specific index path if I'm not on the tableview VC?
I hope that makes sense.
Your data structure probably looks something like this (or should):
Course (.rounds) <------>> Round (.course)
Round (.holes) <------>> Hole (.round)
You press on a row in the list of holes, simply pass the select Hole to the final view controller. If you need any information from any of the superordinate entities, simply use dot notation.
NSString *nameOfCourse = self.hole.round.course.name;
No need to fetch, no need to communicate with any other view controllers! How swell is that?
I have set up a core data model and I would like the ability to be able to tap on an entry and have an blank email pop up with the core data contents included. here is my code for saving the data-
NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:#"Device" inManagedObjectContext:context];
NSNumber *timetickNumber = [NSNumber numberWithInt:timeTick];
NSString *versionString = [NSString stringWithFormat:#"Minutes of %#", self.versionTextField.text];
[newDevice setValue:timetickNumber forKey:#"name"];
[newDevice setValue:versionString forKey:#"version"];
[newDevice setValue:self.companyTextField.text forKey:#"company"];
Thanks in advance!
You can't click (or tap) on a Core Data object, because they do not have any UI and do not respond to UI events. The general flow you want is probably something like:
Figure out what UI element should trigger the email action-- a button, or a table row, or whatever UI element you're using.
In the handler method for taps on that element, create the MFMailComposeViewController. Where this happens depends on what UI element you're using. If your UI element is a button, this happens in whatever method your button calls when tapped. If the UI element is a table row, you probably want to use tableView:didSelectRowAtIndexPath: in your table view's delegate.
In that same method, get a reference to the managed object you want to send. If you fetched it (or created it) earlier, you might already have an instance var that points to it. If not, you may need to fetch it here.
Before displaying the MFMailComposeViewController, configure either the message body (via setMessageBody:isHTML:) or the attachment (via addAttachmentData:mimeType:fileName:) to contain the data from your managed object's data
Display the mail compose view to the user.
I have a newb question, which I have tried unsuccessfully to find answers for on the web. The task is simple: I want to create a core data document-based app but alter the values in some label objects. Using interface builder, I can build the core data model and populate it, using an array controller, table, etc. all without writing any code. So far so good. My test example is to build a core data model with Box entities that have length and width attributes. I would like a label to display the area, i.e. length*width for any geometrically challenged :).
So after browsing around, I've decided I need to create an NSWindowController subclass and use that to update the label when a box in the table is selected. Have attempted this, but have failed. Before I even hook up the label to the window controller, I have a problem. Following the template comments, I added this to Document.m:
- (void)makeWindowControllers
{
NSLog(#"Adding custom Window Controller");
MyWindowController* myWindowController = [[MyWindowController alloc] init];
[self addWindowController:myWindowController];
}
Also added this to the template MyWindowController.m:
- (id)init
{
self = [super initWithWindowNibName:#"MyWindowController"];
return self;
}
The window controller has its own NIB file from Interface builder where I put the table and label etc. The file owner is set to MyWindowController. Probably forgetting other things, but that's what I remember for now.
The log message appears at startup, but then I get an exception "this class is not key value coding-compliant for the key managedObjectContext" before the window appears. I'm guessing that I don't have the window controller hooked up to the document class properly? My other thought is that the array controller is in the window controller nib, not the document nib, so maybe it's looking in the wrong place for the managedObjectContext?
I would try a different approach: Add area to your Box entity as a read-only attribute. Auto-create a Box class with Xcode ("Create NSManagedObject Subclass" menu item in the "Editor" menu when viewing the data model), then add this to Box.h
#property (weak, readonly) NSString * area;
and this to Box.m
- (NSNumber *)area
{
return [NSNumber numberWithDouble:([[self length] doubleValue] - [[self width] doubleValue])];
}
+ (NSSet *)keyPathsForValuesAffectingArea
{
return [NSSet setWithObjects:#"length", #"width", nil];
}
If you do this you can just bind area to a label value like you do for the other Box properties. No need to subclass NSWindowController or watch for changes.
I have a simple maps app with multiple pins on a map view. My intention is to tap a pin, show a callout with an accessory view, push to a Detail View Controller where you can edit that pin/locations details. This all works fine, but once i pop the Detail View Controller the callout on the map view is still there, which i want, but it still has the old uneditied values. How can i refresh/update the callout view once the Detail View Controller is popped?
I am using Core Data with a simple database. I have tried using controllerdidchangecontent, Map View Controller Will Display methods etc but my main problem is identifying which object has been added/updated/deleted and which is the corresponding callout/selected pin.
Any help appreciated...
Not sure if you had find your answer but the way to do it is to extend MKAnnotation class and creating custom annotation and passing them while creating placemarks. Later you can get them from MKAnnotationView's annotation property.
See a good implementation here
http://www.slideshare.net/360conferences/getting-oriented-with-mapkit-everything-you-need-to-get-started-with-the-new-mapping-framework
The only way I could find to update the callout info was to mess directly with the subviews of the callout.
The callout view is the first subview of the annotation view.
In the following example, I update the subtitle.The title label is the 6th and the subtitle is the 7th subview of the callout:
if (myAnnotationView.subviews.count > 0)
((UILabel*)[((UIView*)[myAnnotationView.subviews objectAtIndex:0]).subviews objectAtIndex:7]).text = #"Some example";
i have tableview.In method 'viewWillAppear',it calls a function and function data is stored in an array named 'list'.Now in the method 'numberOfRowsInSection' method it returns the list count...and in another method 'cellForRowAtInexPath' it displays data in cell...now when the view is pushed to the above view using pushViewnavigationcontroller,function is called in viewwillappear and data is shown in tableview..now when i come back to previous view and move to tableview view again...viewWillAppear method is called(i checked using NSLog)...but tableview displays the same previous data..that means methods numberOfRowsInSection,cellForRowAtIndexPath are called only Once at first click..why is it so??and what can be done to load the tableview again and again with changing values of the data..??
I found the solution by myself. Just use [tableview reloadData] in method viewWillAppear.