gpt4 book ai didi

iphone - 单击 MKAnnotationView 中的按钮

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:12:50 24 4
gpt4 key购买 nike

我正在使用 MKMapView 开发一个应用

有一些注释,当你点击注释图像时,会显示详细信息

为此我构建了我的自定义@interface 注释:NSObject 并添加了一个 bool 值“bClicked”

现在我希望用户能够单击详细信息 View 中的详细信息按钮,但我编写的代码无法正常工作:

#pragma mark - MKMapViewDelegate

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
MKAnnotationView *annotationView = nil;


int index = 0;

BOOL bFound = FALSE;

for (int i=0;i<[shopAnnotations count];i++)
{
if (annotation == [shopAnnotations objectAtIndex:i])
{
index = i;
bFound = TRUE;
break;
}
}

if (bFound==FALSE)
{
//ADLog(@"bFound FALSE annotation %p",annotation);
return annotationView; // <-- nil
}


Annotation *shopAnnotation = [shopAnnotations objectAtIndex:index];

annotationView = [_mapView dequeueReusableAnnotationViewWithIdentifier:shopAnnotation.identifier];

if (!annotationView)
annotationView = [[[MKAnnotationView alloc] initWithAnnotation:shopAnnotation reuseIdentifier:shopAnnotation.identifier] autorelease];

for (UIView*view in annotationView.subviews)
{
[view removeFromSuperview];
}

annotationView.image = shopAnnotation.Image;
annotationView.alpha = 1.0f;
annotationView.enabled = YES;
annotationView.canShowCallout = YES;

annotationView.userInteractionEnabled = YES;

if (shopAnnotation.bClicked==TRUE)
{

UIView *detailView = [[UIView alloc ] initWithFrame:CGRectMake(0, -50, 290, 56)];
detailView.userInteractionEnabled = YES;
detailView.superview = annotationView;
[detailView release];

UIImageView *bg = [[UIImageView alloc ] initWithImage:[UIImage imageNamed:@"bulle_carte.png"]];

[bg setFrame:CGRectMake(0, 0, 290, 56)];
bg.superview = detailView;

[bg release];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

[button setFrame:CGRectMake(250, 3, 35, 35)];

[button setImage:[UIImage imageNamed:@"fleche_bleue_d.png"] forState:UIControlStateNormal];
button.tag = [shopAnnotation.identifier intValue];
[button addTarget:self action:@selector(btnDetailPressed:) forControlEvents:UIControlEventTouchUpInside];

button.superview = detailView;

button.userInteractionEnabled = YES;

[annotationView setRightCalloutAccessoryView:detailView];

}


return annotationView;
}

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(@"calloutAccessoryControlTapped");
}

-(void)btnDetailPressed:(id)sender
{
NSLog(@"btnDetailPressed");
}

当我单击按钮或 DetailView 内部时,不会调用/触发 btnDetailPressed 或 calloutAccessoryControlTapped。

有什么建议吗?

最佳答案

使用MKMapViewDelegate的方法mapView:didSelectAnnotationView::

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[YOUR_CUSTOM_CLASS class]]) {
//customize it
}
}

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
if ([view isKindOfClass:[YOUR_CUSTOM_CLASS class]]) {
//do something
}
}

Apple's doc

关于iphone - 单击 MKAnnotationView 中的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13531002/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com