MKMapView - 当点击图钉时,始终在详细视图中显示最后一个项目(在我的 plist 中)

发布于 2024-09-28 11:21:24 字数 2755 浏览 0 评论 0原文

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

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

因此,如果我点击 1 号引脚,详细视图中的标题就是自行车。如果我点击 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];
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

溺渁∝ 2024-10-05 11:21:24

因此,annotationTest 的定义是 RootViewController (不是您的注释)(因此逻辑上,如果您覆盖它,则为最后一个)。
使用 (id) 发送者来获取您的注释数据。

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.

一直在等你来 2024-10-05 11:21:24

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

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

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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文