2012-02-27 10 views
0

MKMapViewでこのマップにピン数が多いアプリがあります。どのピンがタップされたのかを確認するには

すべてのピンはrightCalloutAccessoryViewです。私はこの方法でそれを作成します:

UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
[rightButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside]; 
pinView.rightCalloutAccessoryView = rightButton; 

タップされたピンはどれくらいですか? Thnx

答えて

10

showDetails:メソッドでは、マップビューのselectedAnnotations配列からピンをタップすることができます。代わりにaddTargetをやって実装するので、ところで

//To be safe, may want to check that array has at least one item first. 

id<MKAnnotation> ann = [[mapView selectedAnnotations] objectAtIndex:0]; 

// OR if you have custom annotation class with other properties... 
// (in this case may also want to check class of object first) 

YourAnnotationClass *ann = [[mapView selectedAnnotations] objectAtIndex:0]; 

NSLog(@"ann.title = %@", ann.title); 


:プロパティがNSArrayですが、マップビューは、1つのピンのみを一度に選択することができるため、単に配列の最初の項目を取得しますカスタムメソッドでは、マップビューのcalloutAccessoryControlTappedデリゲートメソッドを使用できます。タップ注釈はviewパラメータで提供されています:

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
    calloutAccessoryControlTapped:(UIControl *)control 
{ 
    NSLog(@"ann.title = %@", view.annotation.title); 
} 

あなたがcalloutAccessoryControlTappedを使用する場合はviewForAnnotationからaddTargetを削除していることを確認します。

+1

2つのアノテーションのタイトルが同じ場合はどうなりますか? –

+0

私はサブクラス化して行うことができますが、簡単な方法はありますか? –

+0

@Anna、これは本当に私を助けました..ありがとう+1 –

関連する問題