【发布时间】:2011-07-22 02:01:35
【问题描述】:
在我的 MapView 中,我在某些位置放置了图钉,现在我想在这些图钉注释上显示一些标题并使其可点击,以便在点击时我可以推送另一个视图。 请帮忙!!!!
【问题讨论】:
标签: iphone objective-c mkmapview
在我的 MapView 中,我在某些位置放置了图钉,现在我想在这些图钉注释上显示一些标题并使其可点击,以便在点击时我可以推送另一个视图。 请帮忙!!!!
【问题讨论】:
标签: iphone objective-c mkmapview
这是代码sn-p:
使用此方法的自定义注释视图
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
if (annotation == mapView.userLocation) return nil;
MKPinAnnotationView *pin = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier: @"asdf"];
if (pin == nil)
{
pin = [[[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: @"asdf"] autorelease];
}
else
{
pin.annotation = annotation;
}
pin.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pin.pinColor = MKPinAnnotationColorRed;
pin.animatesDrop = YES;
[pin setEnabled:YES];
[pin setCanShowCallout:YES];
return pin;
}
点击按钮时调用的委托方法
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
//DetailPg=[[BookMarksDetail alloc]initWithNibName:@"BookMarksDetail" bundle:nil];
//DetailPg.selectRest =[view.annotation title]; //In addition, to get the Title of the AnnotationView.
//DetailPg.m=1;
//[self.navigationController pushViewController:DetailPg animated:YES];
//[DetailPg release];
}
【讨论】:
Apples Docs 中都提供了这个问题的答案,我建议您仔细阅读它们。但是,要进行调用,您需要做的就是设置 title 属性。
对于按钮等你需要使用 MapView Delegate 方法:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
_pin.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
Apple 建议重复使用注释,这是您需要考虑的事项。
【讨论】: