简体   繁体   English

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

[英]MKMapView - Removing annotation causes app crash

Removing annotations from my map view in the following way: 通过以下方式从地图视图中删除注释:

 if ([[self.mapView annotations] count] > 0)
{
    [self.mapView removeAnnotations:[self.mapView annotations]];
}

causes my application to crash with the following exception: 导致我的应用程序崩溃,但出现以下异常:

*** 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.'

The annotations are added in the following way: 注释以以下方式添加:

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


}

My PFAnnotation.h is: 我的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

and my PFAnnotation.m is: 而我的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];
}

I have read in some other threads that, setting the annotation properties from background thread is the reason for the above error. 我在其他一些线程中读到,从后台线程设置注释属性是导致上述错误的原因。 But in my case,it is not so because every thing is performed on the main thread. 但就我而言,并非如此,因为每件事都在主线程上执行。 Please advise. 请指教。

ok..solved it at last!!! 好的..终于解决了!!! I think it was due to the animation provided during addition of annotations. 我认为这是由于添加注释时提供的动画。 since there were a number of annotations that were added back-to-back with animation and also the annotations were removed just before the animation started, there could have been a reference to the released annotation(this is my guess). 由于有大量的注解与动画背靠背添加,并且注解在动画开始之前就被删除了,因此可能有对已发布注解的引用(这是我的猜测)。 Moreover, the removal+addition process was made on each regionDidChangeAnimated call, which could have made a overlap between the removal and addition process. 此外,对每个regionDidChangeAnimated调用都进行了删除+添加过程,这可能会使删除和添加过程重叠。 Anyway, how I solved it was that, I provided a timer which will be fired only after 1 second after every regionDidchangeAnimated to make sure that the user has done with dragging. 无论如何,我如何解决的是,我提供了一个计时器,该计时器仅在每个regionDidchangeAnimated之后的1秒后触发,以确保用户已完成拖动操作。 Thus unnecessary addition+removal of annotations was avoided and I was able to avoid the crash. 因此避免了不必要的注释添加和删除,并且我能够避免崩溃。 Thanks to all guys here for their time taken to support me, especially Guntis Treulands. 感谢所有在这里支持我的人,特别是Guntis Treulands。

In your PFAnnotation class, did you declare both title and subtitle properties as they are in the protocol? 在PFAnnotation类中,您是否按照协议中的说明声明了title和subtitle属性?

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

If Your PFAnnotation really has incorrect setters getters for string values: 如果您的PFAnnotation的字符串值确实有不正确的设置方法获取器:

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

Setter: 二传手:

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

Getter: 吸气剂:

- (NSString*) caption 
{
    return caption;
}

Release: 发布:

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

also - it's easier to provide coordinates in this way: (also works on ios 3.1.3) 同样-以这种方式提供坐标更容易:(也适用于ios 3.1.3)

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

than (only from ios 4) 比(仅适用于ios 4)

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM