Tôi biết lỗi này có liên quan đến quản lý bộ nhớ nhưng tôi phải thừa nhận rằng tôi đã bị lỗi! Được lập trình trong mục tiêu c trong khoảng 3 tuần và tất cả các công cụ bộ nhớ quản lý này là khó hiểu! Điều cơ bản xảy ra là tôi có mapview này trong một tableview. Khi nhấp vào nút quay lại để thoát khỏi chế độ xem bản đồ và quay lại menu chính, tôi nhận được lỗi ở trên. Đây là mã từ tập tin headerEXC_BAD_ACCESS (code = 1) Lỗi trong 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
và tập tin thi hành
#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
Ai có thể làm sáng tỏ cho tôi? :)
Tôi đã thử cho ý kiến ra dòng khác nhau của mã để xem nếu tôi có thể làm việc nó ra nhưng nó là cùng một điều mọi thời gian, như tôi đã nói tôi khá mới này để giúp đỡ bất kỳ, ngay cả với gỡ lỗi, sẽ được đánh giá cao :) (Xin lỗi là một clueless đau ở mặt sau lol) – Craig