的MKMapView - 删除注释导致应用程序崩溃(MKMapView - Removing an

2019-09-18 02:20发布

从下面的方式我的地图视图中删除注释:

 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];
}

我曾经在一些其他的线程,从后台线程设置注释性是上述错误的原因读取。 但对我来说,这是并非如此,因为每一件事情是主线程上执行。 请指教。

Answer 1:

ok..solved它在最后! 我认为这是由于除了注释的过程中提供的动画。 因为有一些添加的后端到回来的动画,也是注释被拆除动画开始之前的注释,有可能已经到了释放的注释引用(这是我的猜测)。 此外,在去除+添加处理对各regionDidChangeAnimated呼叫,这可能提出的删除和添加过程之间的重叠制成。 无论如何,我怎么解决的,是的,我提供这只会在1秒之后被炒鱿鱼后,每regionDidchangeAnimated,以确保用户用拖拽做一个计时器。 因此避免了不必要的加法+移除注释和我能够避免碰撞。 由于这里所有的人对他们的时间来支持我,尤其是冈蒂斯Treulands拍摄。



Answer 2:

在你PFAnnotation类,你同时声明标题和副标题特性,其在协议中?

http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKAnnotation_Protocol/Reference/Reference.html



Answer 3:

如果您PFAnnotation真的有字符串值不正确制定者干将:

从这里: http://cocoadevcentral.com/d/learn_objectivec/

二传手:

- (void) setCaption: (NSString*)input
{
    [caption autorelease];
    caption = [input retain];
}

消气:

- (NSString*) caption 
{
    return caption;
}

发布:

- (void) dealloc
{
    [caption release];
    [super dealloc];
}

也 - 这是更方便地提供这样的坐标:(也适用于iOS 3.1.3)

stationPin.stationPosition = (CLLocationCoordinate2D) {[[aStation valueForKey:@"latitude"] doubleValue], [[aStation valueForKey:@"longitude"] doubleValue]}

比(仅从IOS 4)

stationPin.stationPosition = CLLocationCoordinate2DMake([[aStation valueForKey:@"latitude"] doubleValue], [[aStation valueForKey:@"longitude"] doubleValue]);


Answer 4:

请检查是否将财产“标题”明确去除观察员正在代码在任何地方进行。



文章来源: MKMapView - Removing annotation causes app crash