我有一个带有注释的地图视图,这些注释显示标注。单击标注的公开详细信息按钮时,它会进入一个新视图。
我的 MKAnnotations 是一个自定义类,它实现<MKAnnotation>
。我们将该类称为 MyClass。它们存储在 NSMutableArray 中。在此视图的 viewdidload 期间,我将此数组中的每个 MyClass 对象添加到地图视图的注释中。使用调试器,我可以看到,一旦完成所有这些添加,[self.MapView 注释] 顺序与 NSMutableArray 相同。
现在,我在 mapView:viewForAnnotation: 中设置了另一个断点:并检查 1) 我的 NSMutableArray 和 2) [self.MapView 注释] 的顺序。当然,数组的顺序是相同的。然而,注释的顺序已被打乱。
这对我来说是一个大问题,因为我需要使用用户在下一个视图中选择的 MyClass 的特定实例。又名,我想查看注释,找到它的索引,然后使用它来获取数组中的相同索引。
我现在意识到我可以直接保存注释(来自 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;
}
你打电话时addAnnotation
or addAnnotations
,地图视图将引用添加到其内部注释列表中。
The annotations
的财产MKMapView
只是返回这个内部列表(无论它是什么类型)作为NSArray
.
我不知道文档中有任何地方指出annotations
属性按照您添加注释的顺序返回数组。如果您有showsUserLocation
打开后,即使您没有显式添加该注释,数组也会包含该注释。
您不需要关心也不应该依赖于对象中对象的顺序annotations
财产。
关于代码的一些建议:
- 自从你的
array
包含实现的对象<MKAnnotation>
,您可以通过调用一次性添加所有注释,而不是循环遍历它addAnnotations
(复数)并将数组传递给它
- In
viewForAnnotation
,您设置的所有属性都不依赖于任何特定注释,因此您可以将它们全部设置在if (av == nil)
堵塞。这样您就可以获得最大程度的重用。
- Also in
viewForAnnotation
,之后和之外if
,你应该设置annotation
视图的属性设置为当前注释。这是为了防止视图被另一个注释重用。
- 最后,在
viewForAnnotation
,不要假设annotation
将是类型MyClass
。如果你打开showsUserLocation
,情况不会是这样。将参数声明为更安全id<MKAnnotation>
然后如果有必要检查它的类是什么,然后进行转换。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)