Ios 在mapView区域IDChangeAnimated中获取exec错误访问权限
我正在使用showAnnotations方法在iOS7中的Ios 在mapView区域IDChangeAnimated中获取exec错误访问权限,ios,objective-c,ios7,Ios,Objective C,Ios7,我正在使用showAnnotations方法在iOS7中的MKMapView上显示我的标记。有时它工作正常并显示所有注释,但有时它会提供EXEC\u BAD\u访问错误 这是我的密码 NSArray *annotations = MapView.annotations; _mapNeedsPadding = YES; [MapView showAnnotations:annotations animated:YES]; - (void)mapView:(MKMapView *)mapView
MKMapView
上显示我的标记。有时它工作正常并显示所有注释,但有时它会提供EXEC\u BAD\u访问
错误
这是我的密码
NSArray *annotations = MapView.annotations;
_mapNeedsPadding = YES;
[MapView showAnnotations:annotations animated:YES];
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
if(_mapNeedsPadding){
[mapView setVisibleMapRect:mapView.visibleMapRect edgePadding:UIEdgeInsetsMake(100, 20, 10, 10) animated:YES];
_mapNeedsPadding = NO;
}
}
在显示的代码中,您将获得
EXC\u BAD\u ACCESS
,因为调用setVisibleMapRect
会导致地图视图再次调用RegiondChangeAnimated
,从而启动无限递归
即使您正在使用布尔标志\u mapeedspading
来可能防止这种递归,问题是在调用setVisibleMapRect
后,该标志被设置为NO
(它已经调用了RegiondChangeAnimated
,并且该标志从未设置为NO
)
因此,您的代码调用setVisibleMapRect
,这会导致再次调用regionDidChangeAnimated
,从而导致无限递归,从而导致堆栈溢出,从而导致EXC\u BAD\u ACCESS
“快速修复”是在调用
setVisibleMapRect
之前设置\u mapeedspadding
:
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
if(_mapNeedsPadding){
_mapNeedsPadding = NO; // <-- call BEFORE setVisibleMapRect
[mapView setVisibleMapRect:mapView.visibleMapRect edgePadding:UIEdgeInsetsMake(100, 20, 10, 10) animated:YES];
}
}
您是否正确设置了代理?您是否正确使用强引用
EXC_BAD_ACCESS
通常表示您正在尝试引用已解除分配的实例。我使用的是强引用属性,并且我已将MkMapView对象与xib正确连接。委托设置正确,这就是它调用mapView regiondidchangeanimated委托的原因。我不知道问题是什么问题出现了吗在视图控制器之间进行其他导航后?是,仅当我从一个视图控制器移动到另一个视图控制器时,才会出现此问题。在导航过程中,我正在将mkmapview加载到搜索站。是否尝试在多个视图控制器中使用同一实例?如果是这样,请尝试在导航控制器中设置委托方法。
NSArray *annotations = MapView.annotations;
//_mapNeedsPadding = YES;
//[MapView showAnnotations:annotations animated:YES];
MKMapRect rectForAnns = MKMapRectNull;
for (id<MKAnnotation> ann in annotations)
{
MKMapPoint annPoint = MKMapPointForCoordinate(ann.coordinate);
MKMapRect annRect = MKMapRectMake(annPoint.x, annPoint.y, 1, 1);
if (MKMapRectIsNull(rectForAnns))
rectForAnns = annRect;
else
rectForAnns = MKMapRectUnion(rectForAnns, annRect);
}
UIEdgeInsets rectInsets = UIEdgeInsetsMake(100, 20, 10, 10);
[MapView setVisibleMapRect:rectForAnns edgePadding:rectInsets animated:YES];
//Do NOT implement regionDidChangeAnimated...
//- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
// if(_mapNeedsPadding){
// [mapView setVisibleMapRect:mapView.visibleMapRect edgePadding:UIEdgeInsetsMake(100, 20, 10, 10) animated:YES];
// _mapNeedsPadding = NO;
// }
//}