注释 – iOS 4.2中的地图注释和MKMapView问题?

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

这是问题……

从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,25,25);

} else {

    pinView.annotation = annotation;

}   

// Set up the Right callout
UIButton *myDetailButton = [UIButton buttonWithType:UIButtonTypeDetaildisclosure];
myDetailButton.frame = CGRectMake(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中编译时很好

解决方法

在checkButtonTapped中,使用按钮的标记来标识注释.通过调用自定义方法getPinID在viewForAnnotation中设置按钮的标记.

button标签用作mapView注释数组的索引.目前尚不清楚getPinID方法如何在mapView的注释数组中找出它所处的索引.我不确定假设注释将在数组中的特定索引处是明智的.即使mapView没有随机播放注释,您的应用也可能会不断添加删除注释.

因为您在注释视图中将按钮指定为标注附件,而不是使用按钮标记来标识注释,您可以使用mapView自己的mapView:annotationView:calloutAccessoryControlTapped:delegate方法.

不是在按钮上执行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)部分中创建和设置.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...