MKMapView - 删除注释会导致应用程序崩溃

时间:2012-06-13 09:35:15

标签: iphone ios xcode cocoa-touch ios5

按以下方式从地图视图中删除注释:

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

我在其他一些主题中已经读过,从后台线程设置注释属性是导致上述错误的原因。但就我而言,并非如此,因为每一件事都是在主线程上执行的。请指教。

4 个答案:

答案 0 :(得分:5)

好的......终于解决了!我认为这是由于添加注释时提供的动画。由于有许多注释与动画背靠背添加,并且注释在动画开始之前被删除,因此可能存在对已发布注释的引用(这是我的猜测)。此外,删除+添加过程是在每个regionDidChangeAnimated调用上进行的,这可能会在删除和添加过程之间产生重叠。无论如何,我如何解决这个问题,我提供了一个计时器,它将在每个regionDidchangeAnimated之后仅在1秒后触发,以确保用户完成了拖动操作。因此避免了不必要的添加+删除注释,并且我能够避免崩溃。感谢所有人在这里支持我的时间,尤其是Guntis Treulands。

答案 1 :(得分:1)

在你的PFAnnotation类中,你是否声明了协议中的标题和副标题属性?

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

答案 2 :(得分:1)

如果你的PFAnnotation确实有不正确的字符串值的setter getters:

从这里开始: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]);

答案 3 :(得分:1)

请检查代码中的任何位置是否正在删除属性“title”的观察者。

相关问题