启用和禁用注释拖动(动态)(iOS Mapkit)
我创建了一个mapview,它有一个按钮,可以根据项目要求在“编辑”模式和“拖动”模式之间切换。我意识到,通过在viewForAnnotation中将注释设置为Dragable(可拖动),可以很容易地从创建时拖动注释,但所需的行为不允许这样做。我尝试了几种不同的方法将注释更改为可拖动,但没有成功。第一个想法是循环现有注释,并将每个注释设置为“Dragable”和“selected”,但我收到一个无法识别的选择器发送到实例错误(我尝试实例化一个新注释以传入对象并在循环中重新打印,但我也收到了相同的错误): }启用和禁用注释拖动(动态)(iOS Mapkit),ios,mapkit,Ios,Mapkit,我创建了一个mapview,它有一个按钮,可以根据项目要求在“编辑”模式和“拖动”模式之间切换。我意识到,通过在viewForAnnotation中将注释设置为Dragable(可拖动),可以很容易地从创建时拖动注释,但所需的行为不允许这样做。我尝试了几种不同的方法将注释更改为可拖动,但没有成功。第一个想法是循环现有注释,并将每个注释设置为“Dragable”和“selected”,但我收到一个无法识别的选择器发送到实例错误(我尝试实例化一个新注释以传入对象并在循环中重新打印,但我也收到了相同的
如果我能让它工作的话,第一次尝试似乎是最简单的解决方案。另一方面,使用didSelect方法既麻烦又不合法。我对iOS开发还很陌生,所以如果我在努力学习的时候忽略了一些新手的东西,我很抱歉。我感谢社区能提供的任何见解。多谢 第一种方法优于使用
didSelectAnnotationView
delegate方法
导致“无法识别的选择器”错误的代码的问题是,它在注释对象(类型id
)上调用setSelected:
和setDraggable:
),而不是相应的MKAnnotationView
对象。id
对象没有此类方法,因此会出现“无法识别的选择器”错误
地图视图的注释
数组包含对id
(数据模型)对象的引用,而不是这些注释的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;
//声明一个名为local的短变量以引用当前批注。。。
id ann=[mapView.annotations objectAtIndex:index];
//MKAnnotationView有一个“selected”属性,但文档说不要设置
//这是直接的。相反,请调用注释上的注释。。。
[地图视图注释:ann动画:否];
//要更新批注视图上的可拖动属性,请获取
//使用viewForAnnotation方法的批注的当前视图。。。
MKAnnotationView*av=[注释的地图视图:ann];
av.draggable=编辑模式;
您还必须更新
viewForAnnotation
delegate方法中的代码,以便它还将draggable
设置为editMode
,而不是硬编码的YES
或NO
,这样,如果地图视图需要在您已经在for循环中更新注释后重新创建视图,注释视图将具有正确的值,用于draggable
,感谢您帮助我解决这个问题!我有一种感觉,我试图操纵错误的物体。我唯一需要解决的问题是,当我切换到拖动模式时,我需要在拖动注释之前点击注释一次(而不是立即拖动)。我通过在viewAnnotation中调用setSelected和SetDragTable,而不是annotation.selected和annotation.DragTable,解决了这个问题。
[[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;