【问题标题】:MKMapView - Removing annotation causes app crashMKMapView - 删除注释会导致应用程序崩溃
【发布时间】:2012-06-16 05:52:30
【问题描述】:

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

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

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

【问题讨论】:

  • 请先添加一些代码如何添加注释。!谢谢!
  • @Guntis Treulands 我已经编辑了问题并添加了代码。请看。
  • 不设置stationName和stationLength会怎样?它还会崩溃吗?
  • 好像不设置stationName就不会crash。 (从我目前测试的结果来看)。
  • 这是必不可少的。你永远不应该对 NSString 属性使用保留。 NSArray、NSData、NSDictionary 或任何其他具有可变版本的类也是如此。请参阅此处了解原因:stackoverflow.com/questions/387959/…

标签: iphone ios xcode cocoa-touch ios5


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    在您的 PFAnnotation 类中,您是否按照协议中的方式声明了 title 和 subtitle 属性?

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

    【讨论】:

    • 我已经在问题中添加了类文件。它们是否符合 Apple 的要求? (我不是专业的开发人员,请原谅我)。
    • 我不明白你为什么要覆盖标题/副标题的吸气剂......你应该只声明属性并合成它们;稍后当您创建注释时只需设置它们
    • 由于我是维护人员,我不知道为什么原始开发人员会这样写:)。让我试试你指定的路线。
    • 我是否需要创建与 MKAnnotation 协议声明中相同名称的属性 - 'title'、'subtitle' 和 'coordinate'?
    【解决方案3】:

    如果你的 PFAnnotation 确实有错误的 setter getter 字符串值:

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

    【讨论】:

    • 是的..但这似乎也无济于事:(。
    • 你可以尝试实现这些 mapPin 类文件吗? stackoverflow.com/questions/8180513/… 希望这能解决问题。
    • Nopes :(..我已经实现了链接中指定的类文件,但问题仍然存在..
    • 好的,那么显然问题不在于 PFAnnotation,因为当我在我的项目中实现 PFAnnotation 时 - 删除注释时它没有崩溃。您可以在使用注释的地方发布一些代码吗?
    • 好的,那么问题根本不在PFAnnotation上。我还在我的项目中实现了 PFAnnotation - 它并没有崩溃。那么你能在你使用地图注释的地方显示代码吗?例如在这些函数中会发生什么:? - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id ) 注释 - (void)mapView:(MKMapView *)mp annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control - (void) mapView:(MKMapView *)mapView didSelectAnnotationView:(REVClusterAnnotationView *)view
    【解决方案4】:

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

    【讨论】:

    • 还请尝试通过显式调用“performSelectorOnMainthread”在主线程中强制删除和添加注释。
    猜你喜欢
    • 2010-11-25
    • 1970-01-01
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    • 2021-03-22
    • 2011-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多