我在使用 MKMapView / annotations / UINavigationController 时遇到了一些问题。基本上,我使用 UINavigationController 管理我的视图;我的一个视图包含一个 MKMapView,我使用 addAnnotations 方法在其上添加注释(10 到 200)。
一切正常,除了一件事:如果我在 UINavigationController 上导航“太快”,iphone 模拟器会崩溃,收到“EXC BAD ACCESS”信号。例如,如果我加载包含 MKMapView 的视图并立即按下 UINavigationController 导航栏中的“返回”按钮,我会收到信号。我认为问题出在 addAnnotations 方法上:当我的 MKMapView 加载时,我向它添加注释,但看起来一切都是异步完成的。如果我在按下“返回”按钮之前等待一秒钟,我不会出错,但如果我太快,它就会崩溃。如果我删除 addAnnotations 行,我根本不会出错。我想这是因为我的视图是在 addAnnotations 方法完成工作之前由 UINavigationController 发布的。
有什么好的解决办法吗?我不希望用户等待(例如显示加载视图);我想解决方案可能是更好的内存管理,但我不知道我该怎么做。
if(DEBUG_MODE) { NSLog(@"Creating array of placemarks : begin"); }
self.placemarkCache = [[NSMutableArray alloc] init];
// Loading placemarks for a placemark dictionary
NSArray *sortedKeys = [[self.placemarkDictionary allKeys] sortedArrayUsingSelector:@selector(compare:)];
for (id key in sortedKeys) {
MyPlacemark *currentPlacemark = [self.placemarkDictionary objectForKey:key];
[self.placemarkCache addObject:currentPlacemark];
[currentPlacemark release];
}
if(DEBUG_MODE) { NSLog(@"Creating array of placemarks : done"); }
if(DEBUG_MODE) { NSLog(@"Adding placemarks : begin"); }
[self.mapView addAnnotations:self.placemarkCache];
if(DEBUG_MODE) { NSLog(@"Adding placemarks : done"); }
在此示例中,我在地图上显示任何内容之前收到“添加地标:完成”消息。