gpt4 book ai didi

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

转载 作者:技术小花猫 更新时间:2023-10-29 11:04:30 25 4
gpt4 key购买 nike

通过以下方式从我的 map View 中删除注释:

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

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

最佳答案

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

关于iphone - MKMapView - 删除注释导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11012067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com