我有一个带有按钮的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 <UIKit/UIKit.h>
#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当我切换回tableView时,当annotationView下降时,它通常会崩溃。
发布于 2012-07-30 05:08:04
你是否已经撤除了自己作为地图视图的代表?
self.mapview.delegate = nil;尝试在viewWilldisappear或任何您认为必要的时候删除它,因为如果您已经推送了一个视图控制器并将在稍后返回到该视图,则以后可能会需要它。
简而言之,当视图无法响应委托时,请删除它,这就是您获得EXC_BAD_ACCSS的原因。它向您的视图发送了一条消息,但它已经从内存中释放了。
发布于 2012-06-05 09:03:22
您是否在IBOutlets中发布并设置为零?所以至少你应该做:
- (void)viewDidUnload {
self.mapView = nil;
[super viewDidUnload];
}通常,在ViewDidUnload中,您应该发布从Nib文件创建或在ViewDidLoad方法中分配的任何视图对象,并将其设置为nil。
发布于 2012-06-05 08:20:34
我建议在您将self.whereAmIAnnotation添加到mapView时,检查它是否已经发布。这可能是您收到BAD_ACCESS的原因之一。
https://stackoverflow.com/questions/10893808
复制相似问题