0

我有一个地图视图,上面有几个大头针。引脚正在从 plist 加载经度和纬度。我还添加了一个详细视图,您可以在其中获得有关被点击的引脚的更多信息。但是我的问题是,当我点击一个图钉(discloseButton ofc)并被推送到详细视图时,它始终是加载的最后一个项目数据(来自 plist)。我点击哪个引脚并不重要。

例如:
第 1 项 - 标题:汽车
第 2 项 - 标题:球
第 3 项 - 标题:书
第 4 项 - 标题:自行车

因此,如果我点击 pin nr 1,详细视图中的标题是自行车。如果我点击 pin nr 2,详细视图中的标题是自行车,依此类推。希望你明白这一点:)

(抱歉标题不好,找不到更好的标题。)

谢谢!

如果有帮助,这里有一些代码:

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];
}
4

2 回答 2

0

所以 annotationTest 被定义为 RootViewController (不是你的注释)(如果你覆盖它,逻辑上是最后一个)。使用 (id) 发件人来获取您的注释数据。

于 2010-10-22T11:29:48.433 回答
0

耶,修好了!这是我做的。

- (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];
}
于 2010-10-23T09:51:26.160 回答