11

通过以下方式从我的地图视图中删除注释:

 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

4 回答 4

5

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

于 2012-06-18T12:59:31.187 回答
1

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

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

于 2012-06-13T10:10:28.050 回答
1

如果您的 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]);
于 2012-06-13T10:21:25.097 回答
1

请检查是否在代码中的任何地方都明确删除了属性“title”的观察者。

于 2012-06-13T12:15:34.733 回答