3

我有一个带有图钉的地图视图,当用户选择一个图钉时,它会转到该图钉的详细信息屏幕。我还有一个表格视图,当用户选择一个项目时,它会转到相同类型的详细视图。

这就是问题所在...

从 3.1.3 到 4.1,它似乎工作正常。那就是与引脚匹配的详细视图。但是我有一个用户刚刚升级到 iOS 4.2,并说在 4.1 下它工作得很好,但是在 4.2 中,引脚选择将他带到了不同引脚的细节。但是在表格视图中它仍然可以正常工作。

iOS 4.2 中的 MKMapView 是否有可能改变了 pinID 的选择方式?

加法 - 我已经添加了 viewForAnnotation 和 checkButtonTapped 的相关部分

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

int postTag = 0;

MKPinAnnotationView *pinView = (MKPinAnnotationView *)[eMapView dequeueReusableAnnotationViewWithIdentifier:@"Pin"];

if(pinView == nil) {

    pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Pin"];
    pinView.frame = CGRectMake(0, 0, 25, 25);

} else {

    pinView.annotation = annotation;

}   

// Set up the Right callout
UIButton *myDetailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
myDetailButton.frame = CGRectMake(0, 0, 23, 23);
myDetailButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
myDetailButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

[myDetailButton addTarget:self action:@selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

// Identify which pin is being selected
if ([[annotation title] isEqualToString:@"Current Location"]) {
    postTag = 99999;
} else {
    postTag = [annotation getPinID];

} 

myDetailButton.tag  = postTag;

pinView.rightCalloutAccessoryView = myDetailButton;

pinView.animatesDrop = YES;

// Set to show a callout on the pin
pinView.canShowCallout = YES;

return pinView;
}

// Method to show detail view when the callOut button is selected
- (IBAction) checkButtonTapped: (id) sender {

int nrButtonPressed = ((UIButton *)sender).tag;

if (nrButtonPressed < 99999) {
    if (self.showDetailView == nil) {

        DetailData *tmpViewController = [[DetailData alloc] initWithNibName:@"Detail" bundle:nil];
        self.showDetailView = tmpViewController;
        [tmpViewController release];

    }

    buttonDetail = [[mapView annotations] objectAtIndex:(nrButtonPressed-1)];

    NSMutableArray *tmpArray = [NSMutableArray arrayWithObjects: [buttonDetail getDataI], [buttonDetail getDataII], [buttonDetail getDataIII], [buttonDetail getDataIV], [buttonDetail getDataV], nil];

    self.showDetailView.eventData = [[NSMutableArray alloc] initWithArray:tmpArray copyItems:YES];

    [self.navigationController pushViewController:self.showDetailView animated:YES];

    [self.showDetailView.eventData release];

}

}

如您所见,在 checkButtonTapped 中,我记录了选定的引脚,然后从该引脚的注释中收集数据。问题似乎是 nrButtonPressed 现在不正确。但是在 4.1 中编译时很好

4

2 回答 2

4

在 checkButtonTapped 中,注释是使用按钮的标签来识别的。通过调用自定义方法 getPinID 在 vi​​ewForAnnotation 中设置按钮的标签。

按钮标签用作 mapView 注释数组的索引。目前尚不清楚 getPinID 方法如何确定它在 mapView 的 annotations 数组中的索引。我不确定假设注释将位于数组中的特定索引处是否明智。即使 mapView 不打乱注释,您的应用程序也可能会不断添加和删除注释。

因为您在注解视图中将按钮指定为标注附件,而不是使用按钮标签来标识注解,所以可以使用 mapView 自己的mapView:annotationView:calloutAccessoryControlTapped:委托方法。

不要在按钮上执行 addTarget 并让它调用您的自定义方法,而是删除对 addTarget 的调用并实现 calloutAccessoryControlTapped。

在 calloutAccessoryControlTapped 中,您可以使用 直接访问注解view.annotation。您还可以轻松检查注释是否为“用户位置”:

 - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
    {
        if (view.annotation == mapView.userLocation)
            return;

        buttonDetail = (MyCustomAnnotationClass *)view.annotation;

        //show detail view using buttonDetail...
    }

此外,在 viewForAnnotation 中,即使视图被重用,您每次都在创建一个按钮。因此,重复使用的注释视图可能最终会得到多个按钮。该按钮只能在该if(pinView == nil)部分中创建和设置。

于 2010-12-08T14:48:05.297 回答
0

如果有人愿意看看我现在正在使用什么(并且它正在工作)......

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

    MKPinAnnotationView *pinView = (MKPinAnnotationView *)[eMapView dequeueReusableAnnotationViewWithIdentifier:@"Pin"];

    if(pinView == nil) {

        pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Pin"];
        pinView.frame = CGRectMake(0, 0, 25, 25);

    } else {

        pinView.annotation = annotation;

    }   

    // Set up the Right callout
    UIButton *myDetailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    myDetailButton.frame = CGRectMake(0, 0, 23, 23);
    myDetailButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    myDetailButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

    pinView.rightCalloutAccessoryView = myDetailButton;

    pinView.animatesDrop = YES;

    // Set to show a callout on the pin
    pinView.canShowCallout = YES;

    return pinView;
}

// Added this at the suggestion of aBitObvious on StackOverflow - 120810
- (void)mapView:(MKMapView *)eMapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {

    if (view.annotation == eMapView.userLocation)
        return;

    if (self.showDetailView == nil) {

        DetailData *tmpViewController = [[DetailData alloc] initWithNibName:@"DetailData" bundle:nil];
        self.showDetailView = tmpViewController;
        [tmpViewController release];

    }

    buttonDetail = (MyCustomAnnotationClass *)view.annotation;

    NSMutableArray *tmpArray = [NSMutableArray arrayWithObjects: [buttonDetail getDataI], [buttonDetail getDataII], [buttonDetail getDataIII], [buttonDetail getDataIV], [buttonDetail getDataV], nil];

    self.showDetailView.eventData = [[NSMutableArray alloc] initWithArray:tmpArray copyItems:YES];

    [self.navigationController pushViewController:self.showDetailView animated:YES];

    [self.showDetailView.eventData release];

}

再次感谢!

于 2010-12-09T02:39:33.783 回答