views:

28

answers:

2

Hello,

I have a mapview with a few pins on it. The pins are loading longitude and latitude from a plist. I've also added a detailview where you get more info about the pin that gets tapped. However my problem is that when I tap on a pin (discloseButton ofc) and get pushed to the detailview it's always the last items data that loads (from the plist). It doesn't matter which pin I tap.

For example:
item 1 - title: car
item 2 - title: ball
item 3 - title: book
item 4 - title: bicycle

So if I tap on pin nr 1 the title in the detailview is bicycle. If I tap on pin nr 2 the title in the detailview is bicycle and so on. Hope you get the point :)

(Sorry for the bad title couldn't come up with a better one.)

Thanks!

Here's some code if it helps:

RootViewController.m

- (void)viewDidLoad {
[super viewDidLoad];

    self.title = @"Map";

    map.delegate = self;

    cam = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] 
                                                     pathForResource:@"Cam" 
                                                     ofType:@"plist"]];

    double minLat = [[cam valueForKeyPath:@"@min.latitude"] doubleValue];
    double maxLat = [[cam valueForKeyPath:@"@max.latitude"] doubleValue];
    double minLon = [[cam valueForKeyPath:@"@min.longitude"] doubleValue];
    double maxLon = [[cam valueForKeyPath:@"@max.longitude"] doubleValue];

    MKCoordinateRegion region;
    region.center.latitude = (maxLat + minLat) / 2.0;
    region.center.longitude = (maxLon + minLon) / 2.0;
    region.span.latitudeDelta = (maxLat - minLat) * 1.05;
        region.span.longitudeDelta = (maxLon - minLon) * 1.05;
    map.region = region;

    for (NSDictionary *camDict in cam){
        annotationTest = [[MyAnnotation alloc] initWithDictionary:camDict];
        [map addAnnotation:annotationTest];
        [annotationTest release];
    }
}


// AnnotatioView
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{

     MKPinAnnotationView *annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin"];
    [annView setAnimatesDrop:YES];
    [annView setCanShowCallout:YES];
    [annView setSelected:YES];
    [annView setUserInteractionEnabled: YES];

    UIButton *discloseButton = [UIButton buttonWithType: UIButtonTypeDetailDisclosure];
    [discloseButton addTarget: self action: @selector(showMyView:) forControlEvents:    UIControlEventTouchUpInside];
    annView.rightCalloutAccessoryView = discloseButton;

    return annView;
}

//Push the detailView with some data
- (IBAction)showMyView:(id)sender {
    DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];

    detailViewController.title = annotationTest.title;
    detailViewController.tempAdress = annotationTest.subtitle;
    detailViewController.tempUrl = annotationTest.url;

    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];
}
A: 

So annotationTest is defined is RootViewController (not your annotation) (so logicly the last one if you overwrite it). use the (id) sender to fetch your annotation data.

Sander Backus
Ah I see, but how do I fetch the annotation data? detailViewController.title = annotation.title -- something something?
johannes
I don't know how you created the MyAnnotation class with the dictionary. But something like [sender.dictionary objectForKey:@"title"]
Sander Backus
I created the dictionary this in MyAnnotation.m: - (id) initWithDictionary:(NSDictionary *) dict{ self = [super init]; if (self != nil) { coordinate.latitude = [[dict objectForKey:@"latitude"] doubleValue]; coordinate.longitude = [[dict objectForKey:@"longitude"] doubleValue]; self.title = [dict objectForKey:@"name"]; self.subtitle = [dict objectForKey:@"address"]; } return self;}
johannes
Then just use sender.title
Sander Backus
Do you mean detailViewController.title = sender.title? Because that dosen't work. Sorry if I don't get it. I can't find any good mapview tutorial. :(
johannes
A: 

Yay, fixed it! Here's was I did.

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{

    MKPinAnnotationView *pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin"];
    [pin setAnimatesDrop:YES];
    [pin setCanShowCallout:YES];
    [pin setSelected:YES];
    [pin setUserInteractionEnabled: YES];

    pin.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    return pin;
}

- (void)mapView:(MKMapView *)mv annotationView:(MKAnnotationView *)pin calloutAccessoryControlTapped:(UIControl *)control { 
    DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];

    detailViewController.title = pin.annotation.title;  

    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];
}
johannes