发行与地图标注,并在的MKMapView的iOS 4.2? [英] Issue with Map Annotation and MKMapView in iOS 4.2?

查看:146
本文介绍了发行与地图标注,并在的MKMapView的iOS 4.2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有图钉的地图观点,即当用户选择一个引脚,进入该引脚的细节画面。我也有一个表视图,当用户选择一个项目它转到同一类型的详细信息视图。

I have a map view with pins that when the user selects a pin it goes to a detail screen for that pin. I also have a table view that when the user selects an item it goes to the same type detail view.

这里的问题...

似乎一切工作正常,从3.1.3到4.1。这是详细信息视图与引脚相匹配。但我有谁只是升级到iOS 4.2,并表示,根据4.1,它的工作很好,但在4.2引脚选择把他带对细节的不同PIN的用户。但在查看表中它仍然能正常工作。

It seems to work fine in everything from 3.1.3 to 4.1. That is the detail view matches with the pin. But I have a user who just upgraded to iOS 4.2 and says that under 4.1 it worked fine, but in 4.2 the pin selection takes him to the detail for a different pin. But in the table view it still works fine.

是否有可能,有在的iOS 4.2中的MKMapView这一变化改变了pinID如何选择?

Is it possible that there was a change in MKMapView in iOS 4.2 that changes how the pinID is selected?

加法 - 我增加了viewForAnnotation和checkButtonTapped的相关部分。

Addition - I have added the relevant parts of viewForAnnotation and 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选择录制然后从注释该引脚收集数据的引脚。这个问题似乎是nrButton pressed现在是不正确。但在4.1编译时,它是好的。

As you can see, in checkButtonTapped I record the pin selected then collect the data from the annotation for that pin. The problem seems to be that nrButtonPressed is now incorrect. But it is fine when compiled in 4.1

推荐答案

在checkButtonTapped,注释正在使用该按钮的标签标识。按钮的标签在viewForAnnotation通过调用自定义方法getPinID设置。

In checkButtonTapped, the annotation is being identified using the button's tag. The button's tag is set in viewForAnnotation by calling the custom method getPinID.

按钮标记被用作索引的MapView批注数组。目前尚不清楚该getPinID方法如何它在图形页面的批注数组中的什么指数。我不知道它是明智的,承担注解将是在数组中的特定索引。即使图形页面不洗牌周围的注解,你的应用程序可能会不断地添加和删除注释。

The button tag is used as the index into the mapView annotations array. It's not clear how the getPinID method figures out what index it is at in the mapView's annotations array. I'm not sure it's wise to assume an annotation is going to be at a specific index in the array. Even if mapView doesn't shuffle the annotations around, your app might be constantly adding and removing annotations.

由于要分配的按钮中的标注视图标注的配件,而不是用一个按钮标签来识别注释,你可以使用的MapView自己的图形页面:annotationView:calloutAccessoryControlTapped:委托方法。

Because you are assigning the button as a callout accessory in the annotation view, rather than using a button tag to identify the annotation, you can use the mapView's own mapView:annotationView:calloutAccessoryControlTapped: delegate method.

相反按钮做addTarget并让它调用您的自定义的方法,取出调用addTarget和实施calloutAccessoryControlTapped的。

Instead of doing addTarget on the button and having it call your custom method, remove the call to addTarget and implement calloutAccessoryControlTapped.

在calloutAccessoryControlTapped,可以直接使用访问注释 view.annotation 。你也可以很容易地检查标注的是用户位置

In calloutAccessoryControlTapped, you can directly access the annotation using view.annotation. You can also easily check if the annotation is the "user location":

 - (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,您是在每天即使视图被重新使用的时间创建一个按钮。所以重新使用注解视图可能最终获得在彼此顶部的多个按钮。该按钮只能被创建,并在如果(pinView ==无)部分设置。

Additionally, in viewForAnnotation, you are creating a button every time even if the view is being re-used. So a re-used annotation view probably ends up getting multiple buttons on top of each other. The button should only be created and set in the if(pinView == nil) section.

这篇关于发行与地图标注,并在的MKMapView的iOS 4.2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆