在tableView和mapView之间切换太快时“EXC_BAD_ACCESS”
我有一个带有按钮的tableView来推送mapView。 推和后动作通常很好。 如果我在这两个视图之间快速切换,将出现“EXC_BAD_ACCESS”错误。
MapViewController.m
- (void)viewDidLoad { [super viewDidLoad]; self.mapView.delegate = self; UIButton *btnL = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40.0, 40.0)]; [btnL setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal]; [btnL addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchDown]; self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:btnL] autorelease]; [btnL release]; self.whereAmIAnnotation = [[[WhereAmIAnnotation alloc] init] autorelease]; if (!self.mapView || !self.whereAmIAnnotation) { NSLog(@"mapview : %@", self.mapView); NSLog(@"whereAmIAnnotation : %@",self.whereAmIAnnotation); // will never enter in to here } [self.mapView addAnnotation:self.whereAmIAnnotation]; }
如果我发表评论[self.mapView addAnnotation:self.whereAmIAnnotation];
,再也没有“EXC_BAD_ACCESS”了。
任何答案和评论将不胜感激。 提前致谢!
编辑2
的main.m
#import #import "AppDelegate.h" int main(int argc, char *argv[]) { @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); // "EXC_BAD_ACCESS" error message shows here } }
编辑3:
whereAmIAnnotation在这里声明:
MapViewController.m
@interface AddrCoordinateAdjustMapViewController() @property (retain) WhereAmIAnnotation *whereAmIAnnotation; @end @implementation MapViewController @synthesize whereAmIAnnotation;
编辑4:
错误信息如下:
2012-07-30 15:56:19.735 myApp[13584:707] *** -[MapViewController respondsToSelector:]: message sent to deallocated instance 0x10195e80
当annotationView
正在删除时,当我切换回tableView
时,它通常会崩溃。
您是否已将自己移除为mapview的代表?
self.mapview.delegate = nil;
尝试在viewWilldisappear中删除它,或者在您认为必要时将其删除,因为如果您已经推送了视图控制器并稍后将返回到视图,则可能需要它。
简而言之,当您的视图无法响应委托时,请将其删除,这就是您获得EXC_BAD_ACCSS的原因。 它向您的视图发送了一条消息,但它已经从内存中释放出来。
您是否在viewDidUnload中释放并设置为nil所有IBOutlet? 所以至少你应该这样做:
- (void)viewDidUnload { self.mapView = nil; [super viewDidUnload]; }
通常在ViewDidUnload中,您应该释放并设置为nil,从Nib文件创建或在ViewDidLoad方法中分配的任何视图对象
我建议在将它添加到mapView时检查self.whereAmIAnnotation是否已经发布。 这可能是您收到BAD_ACCESS的一个原因。
我有类似的问题,我的解决方案是从UINavigationController弹出控制器之前从地图中删除所有叠加层。 我正在使用OpenStreetMap。