如何从选定的注释中获取托管对象

时间:2013-02-16 13:45:03

标签: core-data annotations mkmapview cllocationmanager

我有一个模型:事件,我已经将注释视图加载到mapview,但是如何从选定的注释中获取事件管理对象,因此我可以推送视图控制器来显示事件的信息。 viewForAnnotation部分:

- (MKAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id <MKAnnotation>)annotation
{


    if([annotation class] == MKUserLocation.class) {
        //userLocation = annotation;
        return nil;
    }

    REVClusterPin *pin = (REVClusterPin *)annotation;

    MKAnnotationView *annView;

        annView = [aMapView dequeueReusableAnnotationViewWithIdentifier:@"pin"];

        if( !annView )
            annView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                    reuseIdentifier:@"pin"];

        annView.image = [UIImage imageNamed:@"pinpoint.png"];
        annView.canShowCallout = YES;
        UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [rightButton addTarget:self action:@selector(displayViewController:) forControlEvents:UIControlEventTouchUpInside];
        annView.rightCalloutAccessoryView = rightButton;
        annView.calloutOffset = CGPointMake(-6.0, 0.0);
    }
    return annView;
}

和rightCalloutAccessoryView displayViewController部分:

- (void)displayViewController:(id)sender
{
    Annotation *annotation = [self.mapView selectedAnnotations][0];
    EventsViewController *eventsVC = [[EventsViewController alloc] init];
    eventsVC.event = ???
    [self.navigationController pushViewController:eventsVC animated:YES];
}

如何从Annotation * annotation中获取托管对象= [self.mapView selectedAnnotations] [0]?如果我在Annotation中声明一个事件,那么呢?

1 个答案:

答案 0 :(得分:0)

类Annotation是你自己的不是吗?它是否包含用于保留event的属性?如果不是那么它应该。

如果您删除了已分配给rightButton的自定义选择器,并且您已正确设置了委托,那么您应该调用此函数

- (void)mapView:(MKMapView *)map annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    REVClusterPin *annotation = (REVClusterPin *)view.annotation;
    EventsViewController *eventsVC = [[EventsViewController alloc] init];
    eventsVC.event = annotation.event;
    [self.navigationController pushViewController:eventsVC animated:YES];

}