【问题标题】:MKMapView MKAnnotationView ISSUE?MKMapView MKAnnotationView 问题?
【发布时间】:2012-07-27 09:59:05
【问题描述】:

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

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

问题来了……

当我点击按钮时,引脚选择会将他带到不同引脚的详细视图。但是在表格视图中它仍然可以正常工作。任何人请帮我解决这个问题...?

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

【问题讨论】:

    标签: iphone mkmapview mkannotationview


    【解决方案1】:

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

    上述假设不安全,不推荐。例如,如果地图视图由于用户平移或缩放后需要重新显示注释,则绝对可以为同一个注释多次调用委托方法。

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

    至少(但仍然不推荐),您可以将propertyIndex 属性添加到您的注释类并将其设置为等于您正在创建注释的数组索引(例如PropertyReferenceArr 数组)来自。

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

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

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

    @naveen 关于您的addSubview 行的评论也是有效的(尽管它与问题无关)。您绝对应该删除它。

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

    【讨论】:

      【解决方案2】:
      -(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);
      }
      

      然后设置按钮标签,您可以使用注释的标题或副标题进行进一步比较或任何您想要的

      在您使用的代码中

      [newAnnotation addSubview:pinButton]; // i think this is wrong you should only add it to accessory view
      

      【讨论】:

      • 感谢附件视图的建议,但细节视图问题尚未解决...!
      猜你喜欢
      • 1970-01-01
      • 2010-12-14
      • 2014-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多