首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MKMapView MKAnnotationView问题?

MKMapView MKAnnotationView问题?
EN

Stack Overflow用户
提问于 2012-07-27 17:59:05
回答 2查看 785关注 0票数 0

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

我正在通过数组在mapview中添加多个管脚。我已经在附件视图中添加了按钮,

问题是..。

当我点击按钮时,接点选择会将他带到另一个接点的详细视图。但在表视图中它仍然有效,fine..anyone,请帮助我...?

代码语言:javascript
复制
-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation 
{
    MKPinAnnotationView *newAnnotation = nil;
    if([annotation.title isEqualToString:@"You are here."])
    {
        newAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"greenpin"];
        newAnnotation.pinColor = MKPinAnnotationColorGreen;
    }
    else
    {      
        newAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"redpin"];
        newAnnotation.pinColor = MKPinAnnotationColorRed;
        UIButton *pinButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        pinButton.tag = indexvar;
        [newAnnotation addSubview:pinButton];
        newAnnotation.canShowCallout = YES;    
        newAnnotation.rightCalloutAccessoryView = pinButton;
        newAnnotation.calloutOffset = CGPointMake(-5, 5);
        indexvar++;
        return newAnnotation;
        [pinButton release];
    }
    newAnnotation.animatesDrop = YES;
    newAnnotation.canShowCallout = YES;
    return newAnnotation;
}

- (void)mapView:(MKMapView *)wikiMapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 
{
    NSInteger selectedIndex = ((UIButton *)control).tag;
    NSLog(@"Property Index : %d ",selectedIndex);
    DetailView = [[PropertyDetails alloc] initWithNibName:@"PropertyDetails" bundle:nil];
    DetailView.propertyReference = [PropertyReferenceArr objectAtIndex:selectedIndex];
    DetailView.propertyTitle = [propertyTitlesArr objectAtIndex:selectedIndex];
    DetailView.propertyPrice = [propertyPriceArr objectAtIndex:selectedIndex];
    DetailView.propertyBedrooms = [propertyBedroomsArr objectAtIndex:selectedIndex];
    DetailView.propertyLatitude = [propertyLatitudesArr objectAtIndex:selectedIndex];
    DetailView.propertyLongitude = [propertyLongitudesArr objectAtIndex:selectedIndex];
    DetailView.propertyDefaultImage = [PropertyImageArr objectAtIndex:selectedIndex];
    DetailView.propertyStatusId = [PropertyStatusArr objectAtIndex:selectedIndex];
    [self.navigationController pushViewController:DetailView animated:YES];         
    [DetailView release]; 
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-27 18:23:24

代码语言:javascript
复制
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{    
    myAnnotation *annView = view.annotation; // myAnnotation is my custom class for annotations
    // now in annView you have the exact annotation whose accessorybutton is clicked now you can do whatever you want with it.
    NSLog(@"title = %@",annView.title);
}

与设置按钮标记不同,您可以使用注释的标题或副标题进行进一步的比较或您想要的任何东西

在您的代码中使用

代码语言:javascript
复制
[newAnnotation addSubview:pinButton]; // i think this is wrong you should only add it to accessory view
票数 0
EN

Stack Overflow用户

发布于 2012-08-07 03:24:53

viewForAnnotation中,使用ivar计数器(indexvar)设置按钮标记的方式假设委托方法将只被调用一次,并且按照添加注释的顺序(您可能是在某个循环中按索引顺序执行此操作)。

上面的假设是不安全的,不推荐这样做。例如,如果地图视图因为用户平移或缩放而需要在返回视图后重新显示注释,则肯定可以为同一注释多次调用委托方法。

不使用按钮标记,而是将所需的信息嵌入到注释类本身中,并在添加注释时设置该信息。

至少(但仍然不推荐),您可以做的是向注释类添加一个propertyIndex属性,并将其设置为等于要从中创建注释的数组索引(例如PropertyReferenceArr数组)。

基于您在calloutAccessoryControlTapped中使用的大量数组,一种更好的解决方案是创建一个适当的类对象来保存(房地产" properties“对象的)所有属性,然后使用一个数组来保存它们(而不是为每个属性创建一个单独的数组)。

该类本身可以符合MKAnnotation,因此不需要创建单独的显式注释对象(只需将属性对象本身添加到地图中)。

然后在calloutAccessoryControlTapped中,您只需将view.annotation转换为您的自定义类,您就可以立即访问所有注释/属性数据,而不需要任何标记或数组索引。

@naveen对你的addSubview行的评论也是有效的(尽管它与这个问题无关)。你绝对应该把它去掉。

此外,在viewForAnnotation中,在return之后调用[pinButton release];是没有意义的。这段代码永远不会运行(这很好,因为如果你把它放在return之前,应用程序将会崩溃,因为pinButton是自动发布的)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11685537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档