如何触发mapView:didSelectAnnotationView [英] how to fire mapView:didSelectAnnotationView

查看:309
本文介绍了如何触发mapView:didSelectAnnotationView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iPhone开发的新手.我一直在阅读有关如何使Google Maps注解标注窗口接受换行符的几个问题.我阅读的每个教程都要求我触发mapView:didSelectAnnotationView方法.但是我不知道该如何触发.我尝试过的东西包括

I'm new to iPhone development. I've been reading several questions on how to make a google maps annotation callout window accept line breaks. Every tutorial I've read requires me to fire the mapView:didSelectAnnotationView method. But I have no idea how to trigger this. things I've tried include

  • 将方法放入扩展UIViewController的MapViewController.m文件中
  • 将方法放入扩展MKMapView的MapView.m文件中,然后将我的情节提要中的Mapview元素引用为要使用的类

关于xcode,目标c和iphone开发的很多知识我都不了解,所以我无法说出问题出在哪里.

There's so much about xcode, objective c, and iphone development that I don't understand, so i can't tell where my problem lies.

此刻,我的地图确实在所需的位置上绘制了所需的标记.在开始自定义标注框之前,我只需要了解如何触发mapView:didSelectAnnotationViewmapView:viewForAnnotation函数.

At the moment, my map does plot my desired marker on the desired location. I just need to understand how to fire the mapView:didSelectAnnotationView and mapView:viewForAnnotation functions before I can start customizing the call out box.

是否有人逐步说明如何触发这些功能?

Does anyone have step by step instructions on how to trigger these functions?

推荐答案

有点背景

一些注意事项:

A bit of background

A few things to note:

  1. 您不打mapView:didSelectAnnotationView. MKMapView在其委托上调用该函数.换句话说,当您设置MKMapView时,您会说:嘿,听着,您需要告诉我地图上发生的任何事情,请告诉这个人,他会为您处理."该"guy"是委托对象,它需要实现mapView:didSelectAnnotationView(这也是其名称"did select"(即它已经发生而不是"select"的原因)的原因).在一个简单的例子中,委托通常是拥有MKMapView的UIViewController,这将在下面描述.

  1. You don't call mapView:didSelectAnnotationView. The MKMapView calls that function on it's delegate. In other words, when you set up an MKMapView, you tell it: "hey, listen, anytimme you need to tell me what's happening on the map, go tell this guy, he'll handle them for you". That "guy" is the delegate object, and it needs to implement mapView:didSelectAnnotationView (that's also why its name "did select", ie, it already happened, as opposed to "select"). For a simple case, the delegate is often the UIViewController that owns the MKMapView, which is what I'll describe below.

当用户点击您的注释之一时,该方法将被触发.因此,这是一个开始自定义当他们点击注释视图(例如更新选择内容)时应该发生的情况的好地方.

That method will then get triggered when the user taps on one of your annotations. So that's a great spot to start customizing what should happen when they tap on an annotation view (updating a selection, for instance).

但是,如果要自定义要显示的注释,这并不是您想要的,这听起来像是您真正想要的.为此,只是前面几段中有一种不同的方法

It's not, however, what you want if you want to customize what annotation to show, which is what it sounds like you're actually after. For that, there's a different method just a few paragraphs earlier on the same man page: mapView:viewForAnnotation. So substitute this method if you find that mapView:didSelectAnnotationView isn't what you were looking for.

你能做什么

如果您到带标记的地图为止,我想您至少有: *视图控制器(从UIViewController扩展,并且 *您已添加到该视图控制器的视图中的MKMapView,例如,名为mapView

What you can do

If you got as far as a map with a marker, I'm guessing you have at least: * a view controller (extendeding from UIViewController, and * an MKMapView that you've added to the view for that view controller, say named mapView

您要触发的方法被定义为MKMapViewDelegate协议的一部分. 最简单的连接方式是:

The method you want to fire is defined as part of the MKMapViewDelegate protocol. The easiest way to get this wired is to:

  • 使您的UIViewController成为您的MKMapView的委托

  • make your UIViewController the delegate for you MKMapView

  • 用代码表示,例如在viewDidLoad中,您可以在MapViewController.m中执行mapview.delegate = self,或者
  • 在Interface Builder中,您可以将连接从MKMapView委托属性拖动到文件的所有者
  • in code, say in your viewDidLoad, of your MapViewController.m you could do mapview.delegate = self, OR
  • in Interface Builder, you could drag the connection from the the MKMapView delegate property to the file's owner

然后,在MapViewController.m文件中,在UIViewController上定义一个名为mapView:didSelectAnnotationView的方法,就像协议一样声明它:

then, define a method on your UIViewController called mapView:didSelectAnnotationView, declaring it just like the protocol does, in your MapViewController.m file:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
    // whatever you need to do to your annotation and/or map
}

祝你好运!

这篇关于如何触发mapView:didSelectAnnotationView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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