I'm building an iPhone app and my label keeps grabbing the last item in my plist. The first portion of the app displays a map with annotations. The annotations -- latitude and longitude along with a title are all being pulled from the same plist as well.
Here is my code:
#import "myMapViewController.h"
#import "MapViewAnnotation.h"
@interface myMapViewController ()
@property NSArray *states;
//@property NSString *nameString;
//@property NSString *zipString;
//@property NSString *subtitleString;
//@property NSString *zipString;
@end
@implementation myMapViewController
@synthesize mapView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//self.mapView.showsUserLocation = YES; //showsUserLocation = YES;
self.mapView.delegate = self;
[self.mapView setUserTrackingMode:MKUserTrackingModeFollow animated: YES];
[self.mapView addAnnotations:[self createAnnotations]];
//[self zoomToLocation];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSMutableArray *)createAnnotations
{
NSMutableArray *annotations = [[NSMutableArray alloc] init];
NSString *path = [[NSBundle mainBundle]pathForResource:@"States" ofType:@"plist"];
NSArray *rootLevel = [[NSMutableArray alloc]initWithContentsOfFile:path];
int count = rootLevel.count;
for (int i = 0; i < count; i++){
NSDictionary *secondLevel = [rootLevel objectAtIndex:i];
// NSArray *state = [secondLevel valueForKey:@"State"];
NSArray *stores = [secondLevel valueForKey:@"Stores"];
for (NSDictionary *row in stores) {
NSString *latitude = [row objectForKey:@"Latitude"];
NSString *longitude = [row objectForKey:@"Longitude"];
NSString *title = [row objectForKey:@"Name"];
NSString *nameString = [row objectForKey:@"Number"];
NSString *zipString = [row objectForKey:@"Zip"];
NSString *subtitle = [row objectForKey:@"Address1"];
//Create coordinates from the latitude and longitude values
CLLocationCoordinate2D coord;
coord.latitude = latitude.doubleValue;
coord.longitude = longitude.doubleValue;
MapViewAnnotation *annotation = [[MapViewAnnotation alloc] initWithTitle:title AndCoordinate:coord AndZipString:zipString AndNameString:nameString AndSubSubtitle:subtitle];
[annotations addObject:annotation];
//set label
}
}
return annotations;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:@"showStoreDetails"])
{
MKAnnotationView *annotationView = sender;
id<MKAnnotation> ann = annotationView.annotation;
[segue.destinationViewController setNameString:ann.title];
[segue.destinationViewController setZipString:ann.title];
NSLog(@"log %@", annotationView.annotation.subtitle);
}
}
- (MKAnnotationView *)mapView:(MKMapView *)sender viewForAnnotation:(id < MKAnnotation >)annotation
{
static NSString *reuseId = @"StandardPin";
MKPinAnnotationView *aView = (MKPinAnnotationView *)[sender
dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (annotation == mapView.userLocation){
return nil; //default to blue dot
}
if (aView == nil)
{
aView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:reuseId];
aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
aView.canShowCallout = YES;
//aView.animatesDrop = YES;
}
aView.annotation = annotation;
return aView;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control
{
[self performSegueWithIdentifier:@"showStoreDetails" sender:view];
NSLog(@"accessory button tapped for annotation %@", view.annotation.title);
}
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views {
for(MKAnnotationView *annotationView in views) {
if(annotationView.annotation == mv.userLocation) {
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.7;
span.longitudeDelta=0.7;
CLLocationCoordinate2D location=mv.userLocation.coordinate;
region.span=span;
region.center=location;
[mv setRegion:region animated:TRUE];
[mv regionThatFits:region];
}
}
}
@end