我有一個帶有註釋的地圖視圖,這些註釋顯示一個標註。當標註的披露細節按鈕被點擊時,它會進入一個新的視圖。MKMapView註釋更改/丟失順序?
我的MKAnnotations是一個實現了<MKAnnotation>
的自定義類。我們稱之爲類MyClass。它們存儲在一個NSMutableArray中。在此視圖的viewdidload期間,我將此數組中的MyClass的每個對象添加到地圖視圖的註釋中。使用調試器,我可以看到一旦完成所有這些添加,[self.MapView註釋]順序與NSMutableArray相同。
現在我在mapView:viewForAnnotation中設置另一個斷點:並檢查1)我的NSMutableArray和2)[self.MapView註釋]的順序。該數組當然是以相同的順序。但是,註釋的順序已經混亂。
這對我來說是個大問題,因爲我需要使用用戶在下一個視圖中選擇的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;
}
謝謝您的最後一個點。始終學習 – 2012-12-04 23:29:44