0

calloutAccessoryControlTappeddouble にLat値を割り当てLonてボタン アクションで使用しますが、 の値が表示されている間に値ゼロを取得LatLonますcalloutAccessoryControlTapped。それで、私の問題はどこにあるのでしょうか?

H ファイル:

@interface LocationViewController
  double Lat;
  double Lon;
}

M ファイル:

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{


Lat = [[view annotation]coordinate].latitude;
Lon = [[view annotation]coordinate].longitude;

NSLog(@"Lat: %f AND Lon %f", Lat, Lon); //The value is correct
}

- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{

MKPinAnnotationView *pinAnnotation = nil;

if(annotation != locationMap.userLocation)
{
    static NSString *defaultPinID = @"myPin";

    pinAnnotation = (MKPinAnnotationView *)[locationMap dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    if ( pinAnnotation == nil )
        pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];

    pinAnnotation.canShowCallout = YES;
    pinAnnotation.animatesDrop = YES;
    pinAnnotation.pinColor = MKPinAnnotationColorGreen;
    pinAnnotation.enabled = YES;

    //instatiate a detail-disclosure button and set it to appear on right side of annotation
    UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [infoButton addTarget:self action:@selector(infoButton:) forControlEvents:UIControlEventTouchUpInside];
    pinAnnotation.rightCalloutAccessoryView = infoButton;
}
return pinAnnotation;
}

-(void)infoButton:(id)sender{

  NSString *str = [NSString stringWithFormat:@"http://maps.apple.com/maps?saddr=%f,%f&daddr=%f,%f", Lat,Lon,lat1,lon1];

  NSLog(@"Test %f AND %f", Lat, Lon); //Value of both are zero here. 

  NSURL *URL = [NSURL URLWithString:str];

  [[UIApplication sharedApplication] openURL:URL];
}
4

1 に答える 1

2

デリゲートアクセサリ ボタンのカスタム メソッドの両方を実装しないでください。どちらか一方 を実装してください。calloutAccessoryControlTapped

両方を行うと、マップ ビューは両方を呼び出します。この場合、カスタム ボタン メソッドはデリゲート メソッドの前に (およびLatLonが設定される前に) 呼び出されます。

calloutAccessoryControlTappedデリゲート メソッド のみを使用することをお勧めします。

  • 現在のコードinfoButton:calloutAccessoryControlTappedメソッドに移動します
  • infoButton:メソッドを削除します
  • addTargetから行を削除しますviewForAnnotation


推奨されませんが、何らかの理由でデリゲート メソッドのinfoButton:代わりにカスタムを使用する場合:calloutAccessoryControlTapped

  • calloutAccessoryControlTappedデリゲート メソッドを削除する
  • で、マップ ビューinfoButton:のプロパティを使用して、現在選択されている注釈への参照を取得します。selectedAnnotations

カスタム メソッド オプションのコード例については、どのピンがタップされたかを認識する方法を参照してください。

于 2013-08-23T12:22:32.130 に答える