DetailDisclosure按钮未在注释视图中显示 [英] DetailDisclosure button not showing in annotation view
问题描述
出于某种奇怪的原因,详细信息按钮以某种方式停止出现:
For some odd reason the detail button somehow stopped appearing:
- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *pinAnnotation = nil;
if(annotation != mapView.userLocation)
{
MKPinAnnotationView *pinAnnotation = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"sadasdasd"];
if ( pinAnnotation == nil ){
pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"sadasdasd"] autorelease];
/* add detail button */
NSLog(@"Here");
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinAnnotation.rightCalloutAccessoryView = infoButton;
}
}
return pinAnnotation;
}
此处输出. 提前致谢.
Here is output. Thanks in advance.
推荐答案
第一个问题是pinAnnotation
在该方法中声明了两次.
First problem is that pinAnnotation
is declared twice in that method.
在if(annotation != mapView.userLocation)...
块的第一行和第二行中一次.因此,由于从未设置外部变量(导致没有附件的默认MKAnnotationView
标注),所以return
语句返回nil
.
Once in the first line and second in the if(annotation != mapView.userLocation)...
block. Because of this, the return
statement returns nil
because the outer variable is never set (resulting in a default MKAnnotationView
callout with no accessory).
将第二个声明更改为仅一个赋值.
Change the second declaration to just an assignment.
下一个问题是您需要将canShowCallout
设置为YES
,因为MKPinAnnotationView
的默认值为NO
.您可以在设置附件视图后执行以下操作:
Next problem is that you need to set canShowCallout
to YES
because the default is NO
for an MKPinAnnotationView
. You can do this after setting the accessory view:
pinAnnotation.canShowCallout = YES;
以上内容应解决附件按钮未显示的问题.
The above should fix the accessory button not showing.
无关,但是在重用视图时,还需要设置视图的annotation
属性(在出队后不为nil的情况下).因此,将else
块添加到if (pinAnnotation == nil)
:
Unrelated, but you also need to set the view's annotation
property when it is being re-used (in the case when it is not nil after the dequeue). So add an else
block to the if (pinAnnotation == nil)
:
else {
//annotation view being re-used, set annotation to current...
pinAnnotation.annotation = annotation;
}
这篇关于DetailDisclosure按钮未在注释视图中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!