从下面的方式我的地图视图中删除注释:
if ([[self.mapView annotations] count] > 0)
{
[self.mapView removeAnnotations:[self.mapView annotations]];
}
导致我的应用程序有以下例外崩溃:
*** Terminating app due to uncaught exception 'NSRangeException', reason: 'Cannot remove an observer <MKAnnotationContainerView 0xe87b420> for the key path "title" from <PFAnnotation 0x10851230> because it is not registered as an observer.'
这些注释通过以下方式补充说:
CLLocationCoordinate2D pinPosition;
for (int index = 0; index < [array count]; index++)
{
Station *aStation = [array objectAtIndex:index];
PFAnnotation *stationPin = [[PFAnnotation alloc] init]; //StationPinView
pinPosition = CLLocationCoordinate2DMake([[aStation valueForKey:@"latitude"] doubleValue], [[aStation valueForKey:@"longitude"] doubleValue]);
stationPin.stationName = [aStation valueForKey:@"stationName"];
stationPin.stationPosition = pinPosition;
stationPin.stationLength = [aStation valueForKey:@"platformLength"];
[self.mapView addAnnotation:stationPin];
[stationPin release];
}
我PFAnnotation.h是:
@interface PFAnnotation : NSObject <MKAnnotation>
{
NSString *stationName;
CLLocationCoordinate2D stationPosition;
NSNumber *stationLength;
}
@property (nonatomic, retain) NSString *stationName;
@property CLLocationCoordinate2D stationPosition;
@property (nonatomic, retain) NSNumber *stationLength;
@end
我PFAnnotation.m是:
@implementation PFAnnotation
@synthesize stationName;
@synthesize stationPosition;
@synthesize stationLength;
- (CLLocationCoordinate2D)coordinate;
{
return stationPosition;
}
- (NSString *)title
{
return stationName;
}
- (NSString *)subtitle
{
if (stationLength == nil)
return nil;
else
return [NSString stringWithFormat:@"Platform Length: %@ft",stationLength];
}
- (void)dealloc {
[stationName release];
[stationLength release];
[super dealloc];
}
我曾经在一些其他的线程,从后台线程设置注释性是上述错误的原因读取。 但对我来说,这是并非如此,因为每一件事情是主线程上执行。 请指教。