【问题标题】:Custom callout view with button over a MKMapview in iOS 3.0iOS 3.0 中带有 MKMapview 按钮的自定义标注视图
【发布时间】:2014-03-13 05:33:23
【问题描述】:

I created a subclass of MKAnnotationView which when selected display a callout view (similar to the MKPinAnnotationView) which is just a subclass of UIView with a button and text for now.标注被添加到注释视图中。我遇到的问题是我放在标注视图中的按钮从不触发任何操作,它似乎没有响应触摸事件。

我创建一个新的MKAnnotationView 的原因是因为我需要显示更多 比注释视图中的文本和两个按钮,我找不到方法 使用常规的MKPinAnnotationView 标注来实现这一点。

代码:

- (MKAnnotationView *) mapView:(MKMapView *) mapView viewForAnnotation:(id ) annotation {
    if(![annotation isKindOfClass:[CSMapAnnotation class]])
        return nil;

    CSMapAnnotation* csAnnotation = (CSMapAnnotation*)annotation;

    CustomAnnotation *customAnnotationView=[[[CustomAnnotation alloc] initWithAnnotation:annotation reuseIdentifier:nil] autorelease];
    MKPinAnnotationView* pin = nil; pin = [[[MKPinAnnotationView alloc] initWithAnnotation:customAnnotationView reuseIdentifier:@"identifier"] autorelease];
    [pin setPinColor: MKPinAnnotationColorRed ];
    MKAnnotationView *annotationView = pin;

    annotationView.canShowCallout = YES;
    [pin setPinColor:(csAnnotation.annotationType == CSMapAnnotationTypeStart) ? MKPinAnnotationColorGreen : ((csAnnotation.annotationType == CSMapAnnotationTypeCarLocation)? MKPinAnnotationColorPurple:MKPinAnnotationColorRed) ];



    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0,0,85,25)];
    [leftView setBackgroundColor:[UIColor clearColor]];

    UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [shareButton setBackgroundImage:[UIImage imageNamed:@"share.png"] forState:UIControlStateNormal];
    [shareButton setFrame:CGRectMake(19+GAP, 0, 25, 25)];
    [shareButton addTarget:self action:@selector(shareButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [leftView addSubview:shareButton];


    UIButton *setAlertButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [setAlertButton setBackgroundImage:[UIImage imageNamed:@"alert.png"] forState:UIControlStateNormal];
    [setAlertButton setFrame:CGRectMake(shareButton.frame.origin.x+shareButton.frame.size.width+GAP, 0, 25, 25)];
    [setAlertButton addTarget:self action:@selector(alertButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [leftView addSubview:setAlertButton];


    annotationView.rightCalloutAccessoryView = leftView;
    [leftView release];

    return annotationView;
}

-(void)shareButtonAction:(id)sender {
   NSLog(@"%s",_cmd);
}

当我单击 shareButton 时未调用 shareButtonAction 操作...


我也检查了上面的委托,但是当安装到 iPhone 3.0 时它没有被调用。如果我只有一个按钮来右标注,这个委托将调用,但是(在上述情况下)它在添加带有两个按钮的视图时没有调用.它发生在 iPhone 3.0 中。

【问题讨论】:

标签: objective-c ios mkmapview mkannotationview


【解决方案1】:

我不知道您是否知道(或者您是否关心),但您将 MKAnnotationView 传递给请求 MKAnnotation 的方法,这势必会在以后给您带来问题。

也正因为如此,我不知道我的回答是否真的适合你,但是当你点击标注的附件控件时,有一个委托方法会被调用。将自己设置为 mapview 实例委托后,只需实现:

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

【讨论】:

  • 我也检查了上面的委托,但是当安装到 iPhone 3.0 时它没有被调用。如果我只有一个按钮来右标注,这个委托将调用但是(在上述情况下)它没有调用用两个按钮添加视图。它发生在 iPhone 3.0...
  • 因此,使用 MKViewAnnotation 是行不通的——您需要创建一个自定义 UIView,将其放在地图顶部并捕获触摸。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-20
  • 1970-01-01
相关资源
最近更新 更多