私は注釈付きのマップビューを持っており、これらの注釈は吹き出しを表示します。吹き出しの公開の詳細ボタンがクリックされると、新しいビューが表示されます。MKMapViewアノテーションの変更/削除の順序は?
私のMKAnnotationsは<MKAnnotation>
を実装するカスタムクラスです。そのクラスMyClassを呼んでみましょう。それらはNSMutableArrayに格納されます。このビューのviewdidload中に、この配列のMyClassの各オブジェクトをマップビューの注釈に追加します。デバッガを使用すると、この追加がすべて完了すると、[self.MapView注釈]の順序はNSMutableArrayと同じになることがわかります。
mapView:viewForAnnotation:内に別のブレークポイントを設定し、1)NSMutableArrayと2)[self.MapView annotations]の順番を確認します。配列はもちろん同じ順序です。ただし、注釈の順序が乱れています。
これは大きな問題でした。なぜなら、ユーザーが次のビューで選択したMyClassの特定のインスタンスを使用する必要があったからです。 AKA、私は注釈を見て、その索引を見つけて、それを使って配列内の同じ索引を取得したいと思っていました。
私は今、注釈を直接保存できることを認識しました(Androidの背景から来て、これは私にとってとても涼しかったです)。しかし、私はまだ注文が混乱した理由について、概念的には紛失している。誰か助けてくれますか?以下のコード:
- (void)viewDidLoad
{
if([fromString isEqualToString:@"FromList"])
self.navigationItem.hidesBackButton = TRUE;
else {
self.navigationItem.rightBarButtonItem = nil;
}
self.array = [MySingleton getArray];
//set up map
//declare latitude and longitude of map center
CLLocationCoordinate2D center;
center.latitude = 45;
center.longitude = 45;
//declare span of map (height and width in degrees)
MKCoordinateSpan span;
span.latitudeDelta = .4;
span.longitudeDelta = .4;
//add center and span to a region,
//adjust the region to fit in the mapview
//and assign to mapview region
MKCoordinateRegion region;
region.center = center;
region.span = span;
MapView.region = [MapView regionThatFits:region];
for(MyClass *t in self.array){
[MapView addAnnotation:t];
}
[super viewDidLoad];
}
//this is the required method implementation for MKMapView annotations
- (MKAnnotationView *) mapView:(MKMapView *)thisMapView
viewForAnnotation:(MyClass *)annotation
{
static NSString *identifier = @"MyIdentifier";
//the result of the call is being cast (MKPinAnnotationView *) to the correct
//view class or else the compiler complains
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[thisMapView
dequeueReusableAnnotationViewWithIdentifier:identifier];
if(annotationView == nil)
{
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
}
annotationView.pinColor = MKPinAnnotationColorGreen;
//pin drops when it first appears
annotationView.animatesDrop=TRUE;
//tapping the pin produces a gray box which shows title and subtitle
annotationView.canShowCallout = YES;
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annotationView.rightCalloutAccessoryView = infoButton;
return annotationView;
}
は、あなたの最後のポイントをいただき、ありがとうございます。常に学習 –