2010-12-30 7 views
1

showDetailsを送信する注釈を見つける方法を教えてください。showDetailsを送信する注釈を見つける方法

MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc] 
              initWithAnnotation:annotation reuseIdentifier:BridgeAnnotationIdentifier] autorelease]; 
      customPinView.pinColor = MKPinAnnotationColorPurple; 
      customPinView.animatesDrop = YES; 
      customPinView.canShowCallout = YES; 

      // add a detail disclosure button to the callout which will open a new view controller page 
      // 
      // note: you can assign a specific call out accessory view, or as MKMapViewDelegate you can implement: 
      // - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control; 
      // 
      UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
      [rightButton addTarget:self 
          action:@selector(showDetails:) 
        forControlEvents:UIControlEventTouchUpInside]; 
      customPinView.rightCalloutAccessoryView = rightButton; 

      return customPinView; 

- (void)showDetails:(id)sender 
{ 
    some code 
} 

答えて

8

コード内のコメントには答えがあります。カスタムメソッドを使用してaddTargetを呼び出す代わりに、マップビューのcalloutAccessoryControlTappedデリゲートメソッドを使用します。このメソッドでは、アノテーションへの参照を含むアノテーションビューへの参照を取得します。

addTargetへの呼び出しを削除し、とあなたの「showDetails」メソッドを置き換えます

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
    calloutAccessoryControlTapped:(UIControl *)control 
{ 
    MyAnnotationClass *annot = (MyAnnotationClass *)view.annotation; 
    //do something... 
} 
+0

が、彼は1がクリックされた多くの注釈の中で知りたいです! – carbonr

+0

@ carbonr、 'view.annotation'はクリックされたものです。 addTargetの代わりにデリゲートメソッドを使用することをお勧めします。 – Anna

+0

@ carbonr、デリゲートの代わりにaddTargetを使用する必要がある場合、この回答を参照してください。http://stackoverflow.com/a/9876255/467105 – Anna

関連する問題