刪除註釋:的MKMapView - 刪除註釋導致應用程序崩潰從下面的方式我的地圖視圖
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];
}
我已經在一些其他的線程,從後臺線程設置註釋性是上述錯誤的原因讀取。但就我而言,並非如此,因爲每一件事都在主線上進行。請指教。
請首先添加一些代碼,以便如何添加註釋。謝謝! –
@Guntis Treulands我編輯了問題並添加了代碼。請參見。 – DroidHeaven
當你不設置stationName和stationLength時會發生什麼?它仍然崩潰? –