2012-04-03 31 views
2

我已经创建了一个mapview,它具有一个按钮,可以根据项目需求在“编辑”模式和“拖动”模式之间切换。我意识到,通过在viewForAnnotation中设置可拖动的标签,您的注释可以从创建拖放,但是所需的行为不允许这样做。我尝试了几种不同的方式将注释更改为可拖动但没有成功。首先想到的是循环现有的注释,并设置每一个'可拖动'和'选择',但我得到一个无法识别的选择器发送到实例错误(我尝试实例化一个新的注释传入对象和重绘而在循环,但我得到了同样的错误,以及):启用和禁用注释拖动(即时)(iOS Mapkit)

NSLog(@"Array Size: %@", [NSString stringWithFormat:@"%i", [mapView.annotations count]]); 

    for(int index = 0; index < [mapView.annotations count]; index++) { 

     if([[mapView.annotations objectAtIndex:index]isKindOfClass:[locAnno class]]){ 
      NSLog(@"** Location Annotation at Index: %@", [NSString stringWithFormat:@"%i", index]); 
      NSLog(@"* Location Marker: %@", [mapView.annotations objectAtIndex:index]); 
     } 

     if([[mapView.annotations objectAtIndex:index]isKindOfClass:[hydAnno class]]) { 
      NSLog(@"** Hydrant Annotation at Index: %@", [NSString stringWithFormat:@"%i", index]); 
      NSLog(@"* Hydrant Marker: %@", [mapView.annotations objectAtIndex:index]); 

      [[mapView.annotations objectAtIndex:index]setSelected:YES]; 
      [[mapView.annotations objectAtIndex:index]setDraggable:YES]; 
     } 
    } 

的第二个想法是用“didSelectAnnotationView”,并设置选择和拖动时,它的选择注解,并重置属性时模式再次切换回来。这工作,但非常糟糕的事件并不总是火灾和你的左边挖掘注释一次或多次前,将改变的属性:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view { 
NSLog(@"Annotation Selected!"); 
if(!editMode) { 
    view.selected = YES; 
    view.draggable = YES; 
} 

}

第一次尝试似乎是最简单的解决方案,如果我能得到它的工作。另一方面,使用didSelect方法非常麻烦并且非常棘手。我对iOS开发很陌生,所以我很抱歉如果我忽略了一些新手,同时剔除这个问题。我很感谢社区可以提供的任何见解。非常感谢。

回答

3

第一种方法比使用didSelectAnnotationView委托方法要好。

有使“无法识别的选择”的错误代码中的问题是,它是在请注释对象setSelected:setDraggable:(类型id<MKAnnotation>)代替其对应MKAnnotationView物体。 id<MKAnnotation>对象没有这样的方法,所以你得到“无法识别的选择器”错误。

地图视图的annotations数组包含对id<MKAnnotation>(数据模型)对象的引用 - 而不是这些注释的MKAnnotationView对象。

所以,你需要改变这一点:

[[mapView.annotations objectAtIndex:index]setSelected:YES]; 
[[mapView.annotations objectAtIndex:index]setDraggable:YES]; 

到这样的事情:

//Declare a short-named local var to refer to the current annotation... 
id<MKAnnotation> ann = [mapView.annotations objectAtIndex:index]; 

//MKAnnotationView has a "selected" property but the docs say not to set 
//it directly. Instead, call deselectAnnotation on the annotation... 
[mapView deselectAnnotation:ann animated:NO]; 

//To update the draggable property on the annotation view, get the 
//annotation's current view using the viewForAnnotation method... 
MKAnnotationView *av = [mapView viewForAnnotation:ann]; 
av.draggable = editMode; 


还必须在viewForAnnotation委托方法更新代码,以便它也套draggable改为editMode而不是硬编码的YESNO,这样如果地图视图n在之后重新创建注释的视图,您已经在for循环中更新它,注释视图将具有draggable的正确值。

+0

谢谢你帮我解决这个问题!我有一种感觉,我试图操纵错误的对象。我需要解决的唯一问题是,当我切换到拖动模式时,我需要在拖动它之前敲击一次注释(而不是立即拖动)。我通过在viewAnnotation中调用setSelected和setDraggable来解决此问题,而不是annotation.selected和annotation.draggable。 – ninehundredt 2012-04-03 15:30:11