2010-12-08 2 views
3

私は、ユーザーがピンを選択するとそのピンの詳細画面に行くピンを持つマップビューを持っています。また、ユーザーがアイテムを選択すると、同じタイプの詳細ビューに移動するテーブルビューがあります。ここでiOS 4.2のマップアノテーションとMKMapViewの問題?

が問題だ...

3.1.3から4.1に至るまでには正常に動作するようです。ピンと一致する詳細ビューです。しかし、私はiOS 4.2にアップグレードしたばかりのユーザーしかいないし、4.1の下では正常に機能していると言いますが、4.2ではピンの選択によって別のピンの詳細が表示されます。しかし、テーブルビューではまだうまく動作します。

iOS 4.2のMKMapViewで、pinIDの選択方法が変更された可能性はありますか?

追加 - 私はviewForAnnotationの関連する部分を追加し、checkButtonTappedいる

- (MKPinAnnotationView *)mapView:(MKMapView *)eMapView viewForAnnotation:(id <MKAnnotation>)annotation { 

int postTag = 0; 

MKPinAnnotationView *pinView = (MKPinAnnotationView *)[eMapView dequeueReusableAnnotationViewWithIdentifier:@"Pin"]; 

if(pinView == nil) { 

    pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Pin"]; 
    pinView.frame = CGRectMake(0, 0, 25, 25); 

} else { 

    pinView.annotation = annotation; 

} 

// Set up the Right callout 
UIButton *myDetailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
myDetailButton.frame = CGRectMake(0, 0, 23, 23); 
myDetailButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 
myDetailButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; 

[myDetailButton addTarget:self action:@selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; 

// Identify which pin is being selected 
if ([[annotation title] isEqualToString:@"Current Location"]) { 
    postTag = 99999; 
} else { 
    postTag = [annotation getPinID]; 

} 

myDetailButton.tag = postTag; 

pinView.rightCalloutAccessoryView = myDetailButton; 

pinView.animatesDrop = YES; 

// Set to show a callout on the pin 
pinView.canShowCallout = YES; 

return pinView; 
} 

// Method to show detail view when the callOut button is selected 
- (IBAction) checkButtonTapped: (id) sender { 

int nrButtonPressed = ((UIButton *)sender).tag; 

if (nrButtonPressed < 99999) { 
    if (self.showDetailView == nil) { 

     DetailData *tmpViewController = [[DetailData alloc] initWithNibName:@"Detail" bundle:nil]; 
     self.showDetailView = tmpViewController; 
     [tmpViewController release]; 

    } 

    buttonDetail = [[mapView annotations] objectAtIndex:(nrButtonPressed-1)]; 

    NSMutableArray *tmpArray = [NSMutableArray arrayWithObjects: [buttonDetail getDataI], [buttonDetail getDataII], [buttonDetail getDataIII], [buttonDetail getDataIV], [buttonDetail getDataV], nil]; 

    self.showDetailView.eventData = [[NSMutableArray alloc] initWithArray:tmpArray copyItems:YES]; 

    [self.navigationController pushViewController:self.showDetailView animated:YES]; 

    [self.showDetailView.eventData release]; 

} 

}

あなたが見ることができるように、checkButtonTappedに私はそのピンに対する注釈からデータを収集し、選択したピンを記録。問題は、nrButtonPressedが間違っているようです。しかし、コンパイル時には問題ありません。

+0

ピン選択を処理するviewForAnnotationメソッドとコードを表示します。 – Anna

+0

コードを投稿するのはうれしいでしょうが(ちょっと長いですが)、それは問題ではないと思います。その理由は、私が4.1でコンパイルしたバージョンをiPod touchとその上で実行しているからです。私はviewForAnnotationを変更せず、4.2でコンパイルしたときにシミュレータ(iPhone 4.2をエミュレートする)にエラーがあります。ユーザーは古いバージョンのアプリケーションが4.1でうまくいきましたが、4.2に同じバージョンエラーの表示を開始しました。もしあなたがそうしたら私はそれを掲示するので、あなたはまだコードを望んでいますか? – LancDec

+0

コードが非常に長くて複雑な場合は、デバッグを行い、トラブル領域を特定する必要があります。別の小さなプロジェクトで問題を再現してみてください。 Appleの[リリースノート](https://developer.apple.com/library/ios/#releasenotes/General/RN-iOSSDK-4_2/index.html)に記載されている4.2 MapKitの唯一の違いは、 (Notesと既知の問題のMapKitエントリを参照してください)。これはあなたに関連するかもしれません。 – Anna

答えて

4

checkButtonTappedでは、注釈はボタンのタグを使って識別されています。ボタンのタグは、カスタムメソッドgetPinIDを呼び出すことによって、viewForAnnotationで設定されます。

buttonタグは、mapViewアノテーション配列のインデックスとして使用されます。 getPinIDメソッドがmapViewの注釈配列内のどのインデックスにあるかを調べる方法ははっきりしていません。注釈が配列の特定のインデックスにあると仮定することは賢明ではありません。たとえmapViewがアノテーションをシャッフルしていなくても、あなたのアプリケーションはアノテーションを絶えず追加したり削除しているかもしれません。

注釈を識別するためにボタンタグを使用するのではなく、注釈ビューで吹き出しアクセサリとしてボタンを割り当てるため、mapView独自のmapView:annotationView:calloutAccessoryControlTapped:デリゲートメソッドを使用できます。

ボタンでaddTargetを実行してカスタムメソッドを呼び出す代わりに、addTargetの呼び出しを削除してcalloutAccessoryControlTappedを実装します。

calloutAccessoryControlTappedでは、view.annotationを使用して注釈に直接アクセスできます。注釈は、「ユーザーの場所」である場合にも、簡単にチェックすることができます。

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 
    { 
     if (view.annotation == mapView.userLocation) 
      return; 

     buttonDetail = (MyCustomAnnotationClass *)view.annotation; 

     //show detail view using buttonDetail... 
    } 

はさらに、viewForAnnotationでは、ビューが再使用されている場合でも、ボタンを毎回作成しています。したがって、再利用された注釈ビューはおそらく複数のボタンを互いの上に置くことに終わります。このボタンは、if(pinView == nil)セクションに作成して設定する必要があります。

+0

ありがとうございます。私はcalloutAccessoryControlTappedを使って変更を加えました。私は今明らかに見えるが、1時間前にはなかった小さな追加をした。 calloutAccessoryControlTappedメソッドでは、buttonDetailの前にinitWithNibNameを追加する必要があります。 – LancDec

0

誰もが再び...私は今使用しています(と、それが機能している)かを確認する

- (MKPinAnnotationView *)mapView:(MKMapView *)eMapView viewForAnnotation:(id <MKAnnotation>)annotation { 

    MKPinAnnotationView *pinView = (MKPinAnnotationView *)[eMapView dequeueReusableAnnotationViewWithIdentifier:@"Pin"]; 

    if(pinView == nil) { 

     pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Pin"]; 
     pinView.frame = CGRectMake(0, 0, 25, 25); 

    } else { 

     pinView.annotation = annotation; 

    } 

    // Set up the Right callout 
    UIButton *myDetailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    myDetailButton.frame = CGRectMake(0, 0, 23, 23); 
    myDetailButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 
    myDetailButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; 

    pinView.rightCalloutAccessoryView = myDetailButton; 

    pinView.animatesDrop = YES; 

    // Set to show a callout on the pin 
    pinView.canShowCallout = YES; 

    return pinView; 
} 

// Added this at the suggestion of aBitObvious on StackOverflow - 120810 
- (void)mapView:(MKMapView *)eMapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control { 

    if (view.annotation == eMapView.userLocation) 
     return; 

    if (self.showDetailView == nil) { 

     DetailData *tmpViewController = [[DetailData alloc] initWithNibName:@"DetailData" bundle:nil]; 
     self.showDetailView = tmpViewController; 
     [tmpViewController release]; 

    } 

    buttonDetail = (MyCustomAnnotationClass *)view.annotation; 

    NSMutableArray *tmpArray = [NSMutableArray arrayWithObjects: [buttonDetail getDataI], [buttonDetail getDataII], [buttonDetail getDataIII], [buttonDetail getDataIV], [buttonDetail getDataV], nil]; 

    self.showDetailView.eventData = [[NSMutableArray alloc] initWithArray:tmpArray copyItems:YES]; 

    [self.navigationController pushViewController:self.showDetailView animated:YES]; 

    [self.showDetailView.eventData release]; 

} 

感謝を気にしたい場合!

関連する問題