我知道這個錯誤與內存管理有關,但我必須承認我很難過!在目標c編程約3周,所有這些管理內存的東西都令人困惑! 基本上發生的是我在tableview中有這個mapview。當點擊後退按鈕離開地圖視圖並返回到主菜單時,我會看到上面的錯誤。 這裏是頭文件EXC_BAD_ACCESS(code = 1)Xcode中的錯誤
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface MapViewController : UIViewController <MKMapViewDelegate> {
IBOutlet MKMapView* mapView;
BOOL locate;
}
@property (nonatomic, retain) IBOutlet MKMapView* mapView;
@end
和執行文件
#import "MapViewController.h"
@implementation MapViewController
@synthesize mapView;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
mapView = [[MKMapView alloc] initWithFrame:self.view.frame];
mapView.delegate=self;
mapView.showsUserLocation = YES;
[self.view addSubview:mapView];
[self.mapView.userLocation addObserver:self
forKeyPath:@"location"
options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)
context:nil];
locate = YES;
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (locate == YES) {
MKCoordinateRegion region;
region.center = self.mapView.userLocation.coordinate;
MKCoordinateSpan span;
span.latitudeDelta = 0.1;
span.longitudeDelta = 0.1;
region.span = span;
[self.mapView setRegion:region animated:YES];
locate = NO;
}
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)dealloc {
[super dealloc];
[mapView release];
[self.mapView.userLocation removeObserver:self forKeyPath:@"location"];
[self.mapView removeFromSuperview];
self.mapView = nil;
}
@end
任何人能提供一些線索,我的代碼? :)
我試過評論出各種各樣的代碼行,看看我是否可以解決它,但每次都是同樣的事情,正如我所說,我對這個很陌生,所以任何幫助,即使有調試,都將不勝感激:)(對不起,無言以對在背部疼痛lol) – Craig