Tôi có chế độ xem bản đồ có chú thích và các chú thích này hiển thị chú dẫn. Khi nút chi tiết tiết lộ của chú thích được nhấp, nó sẽ phân tách thành một chế độ xem mới.Chú thích thay đổi/mất thứ tự MKMapView?
MKAnnotations của tôi là lớp tùy chỉnh triển khai <MKAnnotation>
. Hãy gọi đó là lớp MyClass. Chúng được lưu trữ trong một NSMutableArray. Trong chế độ xem của chế độ xem này, tôi thêm từng đối tượng của MyClass vào mảng này vào chú thích của chế độ xem bản đồ. Sử dụng trình gỡ lỗi, tôi có thể thấy rằng một khi tất cả việc thêm này được thực hiện, thứ tự [self.MapView annotations] giống với NSMutableArray.
Bây giờ tôi đặt một điểm ngắt khác trong mapView: viewForAnnotation: và kiểm tra thứ tự 1) NSMutableArray của tôi và 2) [self.MapView annotations]. Các mảng là tất nhiên trong cùng một thứ tự. Tuy nhiên, thứ tự của các chú thích đã được tranh giành.
Đây là một vấn đề lớn đối với tôi, vì tôi cần sử dụng trường hợp cụ thể của MyClass mà người dùng đã chọn trong chế độ xem tiếp theo. AKA, tôi muốn xem chú thích, tìm chỉ mục của nó, và sau đó sử dụng nó để lấy cùng một chỉ mục trong mảng.
Tôi hiện đã nhận ra rằng tôi chỉ có thể lưu trực tiếp chú thích (đến từ nền Android, điều này rất tuyệt với tôi). Tuy nhiên, tôi vẫn còn suy nghĩ một cách khái niệm về lý do tại sao thứ tự trở nên tranh giành. Ai đó có thể giúp tôi? Mã bên dưới:
- (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;
}
Cảm ơn bạn đã điểm cuối cùng của bạn. Luôn luôn học –