创建注释数组以便在地图上制作不同的标注视图

发布于 2024-12-09 15:17:49 字数 1174 浏览 1 评论 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 中调用,因此它只会加载适当的信息,并给出到适当位置的指示。

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

I'm working with a MapView and have 30 pin locations placed and everything's working great. I added a button in the callout with a rightCalloutAccessoryView. Here's the button code:

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

This works fine and calls the "rightButtonPressed" method. Here's the implementation of that method:

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

So, as you see, any time I touch any button from all 30 pin callouts, it goes to the same view (DetailViewController). I want each button to have it's own view to switch to (it's where I'm going to place "Directions To Here" and "Directions From Here" etc. as well as the store address and name).

I realize that I could make 30 views and make 30 different methods that could apply to each pin but I know there has to be a more concise code that deals with arrays.

Possible certain arrays could be called in a UILabel on the DetailViewController so it will just load the appropriate information and also give directions to the appropriate location.

This might be a big question and I've looked around for some tutorials but haven't found anything that answers the question exactly. If anyone could get me started (or even point me in the right direction), I'd be totally grateful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

饮惑 2024-12-16 15:17:49

对于注释视图标注附件,最好使用地图视图自己的委托方法 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];
}

With annotation view callout accessories, it's much better to use the map view's own delegate method calloutAccessoryControlTapped to handle the button press instead of using addTarget and your own custom method.

In the calloutAccessoryControlTapped delegate method, you can directly access the annotation that was tapped using view.annotation without having to figure out which annotation in an array or whatever data structure was tapped.

Remove the call to addTarget and replace the rightButtonPressed: method with:

- (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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文