0

我正在使用 MapView 并放置了 30 个引脚位置,并且一切正常。我在标注中添加了一个带有 rightCalloutAccessoryView 的按钮。这是按钮代码:

UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    pinView.rightCalloutAccessoryView = rightButton;
    [rightButton addTarget:self action:@selector(rightButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    return pinView;
}

这工作正常并调用“rightButtonPressed”方法。这是该方法的实现:

-(IBAction) rightButtonPressed:(id)sender
{
    NSLog(@"button clicked");
    MapViewController *thisMap = (MapViewController *)[[UIApplication sharedApplication] delegate];
    DetailViewController *dvc = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil];
    [thisMap switchViews:self.view toView:dvc.view];
}

因此,如您所见,每当我从所有 30 个引脚标注中触摸任何按钮时,它都会进入同一个视图 (DetailViewController)。我希望每个按钮都有自己的视图来切换(这是我要放置“到这里的路线”和“从这里的路线”等以及商店地址和名称的地方)。

我意识到我可以制作 30 个视图并制作 30 种不同的方法,这些方法可以应用于每个引脚,但我知道必须有一个更简洁的代码来处理数组。

可能的某些数组可以在 DetailViewController 上的 UILabel 中调用,因此它只会加载适当的信息并指示到适当的位置。

这可能是一个大问题,我四处寻找一些教程,但没有找到任何可以准确回答这个问题的东西。如果有人能让我开始(或者甚至指出我正确的方向),我将不胜感激。

4

1 回答 1

1

使用注释视图标注附件,最好使用地图视图自己的委托方法calloutAccessoryControlTapped来处理按钮按下,而不是使用addTarget您自己的自定义方法。

calloutAccessoryControlTapped委托方法中,您可以直接访问使用的注解,view.annotation而无需确定数组中的哪个注解或任何数据结构。

删除对的调用addTarget并将rightButtonPressed:方法替换为:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
            calloutAccessoryControlTapped:(UIControl *)control
{
    //cast the plain view.annotation to your custom class so you can
    //easily access your custom annotation class' properties...
    YourAnnotationClass *annotationTapped = (YourAnnotationClass *)view.annotation;

    NSLog(@"button clicked on annotation %@", annotationTapped);

    MapViewController *thisMap = (MapViewController *)[[UIApplication sharedApplication] delegate];
    DetailViewController *dvc = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil];

    //annotationTapped can be passed to the DetailViewController
    //or just the properties needed can be passed...
    //Example line below assumes you add a annotation property to DetailViewController.
    dvc.annotation = annotationTapped;

    [thisMap switchViews:self.view toView:dvc.view];
}
于 2011-10-14T17:03:03.927 回答