【问题标题】:MKAnnotationView is losing reference to MKAnnotationMKAnnotationView 正在失去对 MKAnnotation 的引用
【发布时间】:2013-09-24 03:47:43
【问题描述】:

在我正在开发的应用程序中,用户被指示按下按钮将 MKAnnotations 拖放到地图上。他们会丢弃 2 或 3 个引脚,当引脚添加到 didAddAnnotationViews 时,每个引脚都会保存到 @property,因为稍后我需要对其进行引用,并且我需要知道它是哪个引脚 - 引脚 1、2、或 3(它们被丢弃的顺序)。

我正在使用自定义 MKAnnotation 和 MKAnnotationView 类为每个注释添加一些 NSString,我不确定这是否重要。

我正在创建 3 个这样的属性:

@property (nonatomic, strong) CustomAnnotationView *ann1;
@property (nonatomic, strong) CustomAnnotationView *ann2;
@property (nonatomic, strong) CustomAnnotationView *ann3;

这是我的didAddAnnotationViews

- (void)mapView:(MKMapView *)aMapView didAddAnnotationViews:(NSArray *)views
{
    for(MKAnnotationView *view in views)
    {
        if(![view.annotation isKindOfClass:[MKUserLocation class]])
        {
            CustomAnnotationView *newAnnView = (CustomAnnotationView*)view;

            if(newAnnView.type == CustomType1)
            {
                ann1 = newAnnView;
            }
            else if(newAnnView.type == CustomType2)
            {
                ann2 = newAnnView;
            }
            else if(newAnnView.type == CustomType3)
            {
                ann3 = newAnnView;
            }
        }
    }
}

另外,这是我的viewForAnnotation 方法:

- (MKAnnotationView *)mapView:(MKMapView *)pMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if([annotation class] == MKUserLocation.class)
    {
        return nil;
    }

    CustomAnnotationView *annotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"WayPoint"];

    annotationView.canShowCallout = YES;

    annotationView.draggable = YES;
    [annotationView setSelected:YES animated:YES];

    [annotationView setRightCalloutAccessoryView:customCalloutButton];

    return annotationView;
}

现在,最终,我需要保存这些注释的坐标,这就是问题所在。有时,但只是偶尔,ann1.annotation.coordinate.latitudeann1.annotation.coordinate.longitude 都为 0.0(这发生在 ann1、ann2 或 ann3 上,仅使用 ann1 作为示例)!为什么会这样?我感觉这与对象引用问题有关,因为 MKAnnotationView 仍然完好无损,但注释已被清除。也许我用 ann1 = newAnnView 分配引用很糟糕?我应该使用viewForAnnotation吗?

有没有人发现我做错了什么?

更新

我查看了我的 MKAnnotation 子类,我注意到虽然我根据文档定义了一个坐标属性,但我没有在我的实现文件中 @synthesizing 它。我现在已经添加了这一点,但我还无法复制这个问题......如果这最终成为“修复”,我仍然很困惑为什么我的代码大部分时间都可以在没有 @synthesize 的情况下工作.也许我并没有真正解决它,我让自己失望了。

【问题讨论】:

    标签: ios objective-c mkmapview mkannotation mkannotationview


    【解决方案1】:

    我不认为你应该这样使用didAddAnnotationViews。通常流程如下:

    1. 创建一个MKAnnotation,或者它的子类的一个实例
    2. 分配你提到的字符串
    3. 致电[mapView addAnnotation:myAnnotation]
    4. viewForAnnotation 中创建MKAnnotationView(或CustomAnnotationView( 基于作为参数提供的annotation
    5. 当您需要保存坐标时,您可以循环遍历mapView.annotations 数组,或者如果您保留了名为 ann1、ann2、ann3 的 thre3 个变量,则可以将它们一一保存。

    当然,如果您找到了更好的方法,或者这不适合您的应用中的其他内容,您不需要使用它,但这是我迄今为止看到的唯一流程。

    【讨论】:

    • 我建议你把这个改成通常的方法。存储 annotations 以供重复使用,而不是查看。在 MVC 范例中,视图不保存数据,它们只是显示有关模型的内容。在这种情况下,MKAnnotations 是您的模型。
    • ^ 上面的评论是很久很久以前为我解决这个问题的=)。存储模型,而不是视图,呵呵。这是我还是 obj-c 新手的时候。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-30
    相关资源
    最近更新 更多