2

サブクラスを作成しました。このサブクラスを選択すると、今のところボタンとテキストをMKAnnotationView含むサブクラスであるコールアウトビュー(MKPinAnnotationViewに類似)が表示されます。UIViewコールアウトが注釈ビューに追加されます。私が抱えている問題は、コールアウトビューに配置したボタンがアクションをトリガーしないことです。タッチイベントに応答していないようです。

新しいものを作成する理由MKAnnotationViewは、注釈ビューにテキストと2つ以上のボタンを表示する必要があり、通常の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にインストールした場合は呼び出されません。右のコールアウトにボタンが1つしかない場合、このデリゲートは呼び出されますが、(上記の場合)2つのボタンでビューを追加すると呼び出されません。それはiPhone3.0で起こっています。

4

1 に答える 1

1

あなたが気づいているかどうか (または気にかけているかどうか) はわかりませんが、MKAnnotation を要求するメソッドに MKAnnotationView を渡しているため、後で問題が発生する可能性があります。

また、そのため、私の答えが実際に機能するかどうかはわかりませんが、コールアウトのアクセサリ コントロールをタップするとゲルが呼び出されるデリゲート メソッドがあります。自分自身を mapview インスタンス デリゲートとして設定したら、次のように実装します。

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
   NSLog (@"Callout accessory control tapped");
}
于 2011-02-18T10:11:57.397 に答える