【问题标题】:When to link annotation property in viewForAnnotation method何时在 viewForAnnotation 方法中链接注解属性
【发布时间】:2016-02-10 15:19:38
【问题描述】:

我在 Apple 的Location and Maps Programming Guide 中看到了这段代码:

- (MKAnnotationView *)mapView:(MKMapView *)mapView
                      viewForAnnotation:(id <MKAnnotation>)annotation
{
    // If the annotation is the user location, just return nil.
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    // Handle any custom annotations.
    if ([annotation isKindOfClass:[MyCustomAnnotation class]])
    {
        // Try to dequeue an existing pin view first.
        MKPinAnnotationView*    pinView = (MKPinAnnotationView*)[mapView
        dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];

        if (!pinView)
        {
            // If an existing pin view was not available, create one.
            pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
                       reuseIdentifier:@"CustomPinAnnotationView"];
            pinView.pinColor = MKPinAnnotationColorRed;
            pinView.animatesDrop = YES;
            pinView.canShowCallout = YES;

            // If appropriate, customize the callout by adding accessory views (code not shown).
        }
        else
            pinView.annotation = annotation;

        return pinView;
    }

    return nil;
}

我对此部分有疑问:

else
    pinView.annotation = annotation;

我在 Stackoverflow answer 中看到了相同的代码:

无关,但还需要设置视图的annotation属性 当它被重新使用时(在它不是 nil 的情况下 出队)。所以在if (pinAnnotation == nil) 中添加一个else 块:

else {
    //annotation view being re-used, set annotation to current...
    pinAnnotation.annotation = annotation;
}

为什么要在else块中设置注解属性?无论视图是否被重用,不应该设置注释吗?

【问题讨论】:

    标签: ios mapkit mapkitannotation


    【解决方案1】:

    您使用注释初始化MKPinAnnotationView,这样else 就不会设置两次。设置两次完全没问题,但效率当然低。

    【讨论】:

    • 天哪,我完全错过了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    相关资源
    最近更新 更多