无法选择注释? [英] Can't select an annotation?

查看:16
本文介绍了无法选择注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在地图视图上有一个注释.我可以以编程方式选择它,但我点击它没有任何反应.你可以帮帮我吗?有没有人遇到过类似的问题?这是设置注释的方法:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation{MKAnnotationView *aView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"MapVC"];如果(!aView){aView = [[MKPinAnnotationView alloc] initWithAnnotation:注释重用标识符:@"MapVC"];aView.canShowCallout = YES;aView.draggable=YES;aView.leftCalloutAccessoryView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];//可以在这里放一个 rightCalloutAccessoryView}aView.annotation = annotation;[(UIImageView *)aView.leftCalloutAccessoryView setImage:nil];返回一个视图;}

并将它们添加到地图视图:

- (void)updateMapView{if (self.mapView.annotations) [self.mapView removeAnnotations:self.mapView.annotations];if (self.annotation) [self.mapView addAnnotation:self.annotation];}

以及对注释的按下做出反应的方法:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)aView{NSLog(@"确实选择了注解");}

顺便说一下,方法 [self.mapView selectAnnotation:annotation] 有效,但没有提供标注(我用断点检查了它).虽然只是录制注释不会(再次通过断点检查).

解决方案

如果注解的 title 为 nil 或空白,标注将不会显示(即使其他所有设置都正确,包括 canShowCallout).

当你点击一个注解时,didSelectAnnotationView 委托方法将被调用,如果注解有一个非空的title,标注将被显示.>


关于您在评论中的问题:

<块引用>

...是不是我有一个单独的类来包装我的所有数据,我的注释类包含该数据类的实例?

这没有什么问题.
如果您希望将与地图相关的逻辑与基类分开,这很好,对于复杂的应用程序来说可能是个好主意,其中基数据类可能不仅仅用于注释.

如果您的应用非常简单并且数据仅用于注释,您可以保持非常简单并将两者结合,但这不是必需的.

只要你坚持使用直接引用而不是尝试,例如,使用数组索引或视图/按钮标签从注释链接回某些数据对象,正确"的类实现取决于什么适用于您的应用.

I have a single annotation on a map view. I can select it programmaticly, but the I tap it nothing happens. Could you help me? Did anyone encounter similar problem? Here is mehod for setting up anotations:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    MKAnnotationView *aView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"MapVC"];
    if (!aView) {
        aView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MapVC"];
        aView.canShowCallout = YES;
        aView.draggable=YES;
        aView.leftCalloutAccessoryView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
        // could put a rightCalloutAccessoryView here
    }
    aView.annotation = annotation;
    [(UIImageView *)aView.leftCalloutAccessoryView setImage:nil];
    return aView;
}

And adding them to map view:

- (void)updateMapView
{
    if (self.mapView.annotations) [self.mapView removeAnnotations:self.mapView.annotations];
    if (self.annotation) [self.mapView addAnnotation:self.annotation];
}

And mehod reacting to pressing of annotations:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)aView
{
    NSLog(@"did select annotation");
}

By the way, method [self.mapView selectAnnotation:annotation] works, but doesn't put up a callout(i checked it with breakpoint). While just taping annotation doesn't(again cheked through breakpoints).

解决方案

If an annotation's title is nil or blank, the callout will not show (even if everything else is set properly including canShowCallout).

When you tap on an annotation, the didSelectAnnotationView delegate method will get called and if the annotation has a non-blank title, the callout will be displayed.


Regarding your question in the comments:

...is it right I have a seperate class to wrap all my data in to, my annotation class contains an instance of that data class?

There's nothing wrong with this.
If you want to keep map-related logic separate from the base class, that's fine and probably a good idea for a complex app where the base data class may be used for more than just annotations.

If your app is very simple and the data is only used for annotations, you could keep things very simple and combine the two but it's not a requirement.

As long as you stick to using direct references instead of trying to, for example, use array indexes or view/button tags to link back to some data object from the annotation, the "right" class implementation depends on what works for your app.

这篇关于无法选择注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆