右附件标注方法和实施 [英] Right Callout Accessory method and implementation
问题描述
我想知道是否有人能告诉我如何右附件标注向地图添加注释。一切我尝试似乎并没有在任何地方得到,所以任何帮助将是AP preciated。
修改
我已经试过这条线code,但没有任何不同发生了注释。
- (MKAnnotationView *)的MapView:(*的MKMapView)发送viewForAnnotation:(ID< MKAnnotation>)注释
{
MKAnnotationView * aView = [[MKPinAnnotationView页头] initWithAnnotation:注释reuseIdentifier:@];
aView.rightCalloutAccessoryView = [UIButton的buttonWithType:UIButtonTypeDetailDisclosure];
aView.canShowCallout = YES;
aView.annotation =注释;
返回aView;
}
方法名是错误的。它应该是图形页面
用大写 V
:
- (MKAnnotationView *)的MapView:(*的MKMapView)发件人
viewForAnnotation:(ID< MKAnnotation>)注释
目标-C是区分大小写的。
如果该方法仍不会被调用,那么另一个问题是地图视图的委托
未设置。在code,将其设置为自
或在Interface Builder附加委托文件的所有者。
另外,还要确保你添加它,否则仍然标注不会显示之前设置注释的标题
。
以上的变化应该可以解决附件按钮没有出现。
结果
其他一些不相关的建议...
在 viewForAnnotation
,你应该支持的注解视图再利用,通过调用 dequeueReusableAnnotationViewWithIdentifier
:
- (MKAnnotationView *)的MapView:(*的MKMapView)发送viewForAnnotation:(ID< MKAnnotation>)注释
{
静态的NSString * reuseId = @StandardPin MKPinAnnotationView * aView =(MKPinAnnotationView *)[发送者
dequeueReusableAnnotationViewWithIdentifier:reuseId];
如果(aView ==无)
{
aView = [[[MKPinAnnotationView页头] initWithAnnotation:注释
reuseIdentifier:reuseId]自动释放];
aView.rightCalloutAccessoryView = [UIButton的buttonWithType:UIButtonTypeDetailDisclosure];
aView.canShowCallout = YES;
} aView.annotation =注释; 返回aView;
}
如果你的项目使用ARC,删除自动释放
。
结果
顺便说一句,以应对附件按钮preSS,落实 calloutAccessoryControlTapped
委托方法:
- (空)的MapView:(*的MKMapView)的MapView annotationView:(MKAnnotationView *)查看
calloutAccessoryControlTapped:(UIControl *)控制
{
的NSLog(@附件按钮横置以注解%@,view.annotation);
}
I was wondering if anyone could tell me how to add a right callout accessory to a map annotation. everything i try doesn't seem to be getting anywhere, so any help would be appreciated.
EDIT
I have tried this line of code but nothing different happens to the annotation.
- (MKAnnotationView *)mapview:(MKMapView *)sender viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *aView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@""];
aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
aView.canShowCallout = YES;
aView.annotation = annotation;
return aView;
}
The method name is wrong. It should be mapView
with a capital V
:
- (MKAnnotationView *)mapView:(MKMapView *)sender
viewForAnnotation:(id <MKAnnotation>)annotation
Objective-C is case-sensitive.
If the method still doesn't get called, then the other problem is that the map view's delegate
is not set. In code, set it to self
or in Interface Builder attach the delegate to File's Owner.
Also make sure you set the title
of the annotation before adding it otherwise the callout still won't show.
The above changes should fix the accessory button not appearing.
Some other unrelated suggestions...
In viewForAnnotation
, you should support annotation view re-use by calling dequeueReusableAnnotationViewWithIdentifier
:
- (MKAnnotationView *)mapView:(MKMapView *)sender viewForAnnotation:(id < MKAnnotation >)annotation
{
static NSString *reuseId = @"StandardPin";
MKPinAnnotationView *aView = (MKPinAnnotationView *)[sender
dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (aView == nil)
{
aView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:reuseId] autorelease];
aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
aView.canShowCallout = YES;
}
aView.annotation = annotation;
return aView;
}
If your project uses ARC, remove the autorelease
.
By the way, to respond to the accessory button press, implement the calloutAccessoryControlTapped
delegate method:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(@"accessory button tapped for annotation %@", view.annotation);
}
这篇关于右附件标注方法和实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!