Ios 创建注释数组以便在地图上创建不同的详图索引视图

Ios 创建注释数组以便在地图上创建不同的详图索引视图,ios,arrays,mkmapview,callouts,Ios,Arrays,Mkmapview,Callouts,我正在使用MapView,放置了30个pin位置,一切正常。我在详图索引中添加了一个按钮,该按钮带有rightCalloutAccessoryView。以下是按钮代码: UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; pinView.rightCalloutAccessoryView = rightButton; [rightButton addTarget:self

我正在使用MapView,放置了30个pin位置,一切正常。我在详图索引中添加了一个按钮,该按钮带有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中调用,因此它将只加载适当的信息,并指示适当的位置


这可能是一个很大的问题,我已经到处找了一些教程,但没有找到任何能准确回答这个问题的。如果有人能让我开始(甚至给我指出正确的方向),我将万分感激

对于注释视图调用附件,最好使用地图视图自己的委托方法
calloaccessorycontrol
来处理按钮按下,而不是使用
addTarget
和自己的自定义方法

calloutAccessoryControlTapped
delegate方法中,您可以使用
view.annotation
直接访问已点击的注释,而无需找出数组中的哪个注释或点击的任何数据结构

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

- (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];
}

伟大的这比我想象的要容易得多(我仍在熟悉map view代理的方法)。我现在遇到的一个问题(我以前的方法也遇到过这个问题)是程序和所有东西都可以正常工作,但是在“[thisMap switchview:self.view to view:dvc.view];”行上它向我发出警告,“MapViewController”可能不会响应“switchViews:toView:”即使响应。我只是想现在就把它弄清楚,以防以后成为问题。非常感谢!确保您已经在MapViewController.h(接口)文件中声明了switchViews方法。我很困惑,因为switchViews方法是在“-(void)mapView”方法中调用的,声明它的语法是什么?我知道如何自己声明一个方法,但是像这样的东西呢?(对不起!我还在学习…)不管从哪里打过来。警告是说MapViewController没有声明这样的方法,因此调用它可能会导致运行时崩溃。在MapViewController.m中,有switchViews方法。只需将方法标题(例如
-(void)switchview:(UIView*)从视图添加到视图:(UIView*)到视图;
)并将其添加到@end.Oh之前的MapViewController.h文件中就行了,这很有意义。我感谢你的帮助!