Objective c 如何启动mapView:didSelectAnnotationView
我对iPhone开发还不熟悉。我一直在阅读一些关于如何使google maps注释标注窗口接受换行的问题。我读过的每个教程都要求我启动Objective c 如何启动mapView:didSelectAnnotationView,objective-c,mkmapview,Objective C,Mkmapview,我对iPhone开发还不熟悉。我一直在阅读一些关于如何使google maps注释标注窗口接受换行的问题。我读过的每个教程都要求我启动mapView:didSelectAnnotationView方法。但我不知道怎么触发这个。我试过的东西包括 将该方法放入扩展UIViewController的my MapViewController.m文件中 将该方法放入一个MapView.m文件中,该文件扩展了MKMapView,然后让我的故事板中的MyMapView元素引用它作为要使用的类 关于xcod
mapView:didSelectAnnotationView
方法。但我不知道怎么触发这个。我试过的东西包括
- 将该方法放入扩展UIViewController的my MapViewController.m文件中
- 将该方法放入一个MapView.m文件中,该文件扩展了MKMapView,然后让我的故事板中的MyMapView元素引用它作为要使用的类
mapView:didSelectAnnotationView
和mapView:viewForAnnotation
功能,然后才能开始自定义调用框
是否有人有关于如何触发这些功能的分步说明?
地图视图:didSelectAnnotationView
是地图视图的一种委托方法,您可以在此处阅读:
您不需要调用它,映射视图将“自行”调用它,并将其发送给注册为其代理的每个视图/视图控制器
您需要做什么
基本上,您需要在.h文件中添加MKMapViewDelegate,如下所示:
@interface someViewController : UIViewController <MKMapViewDelegate>
从这一点上,控制器将能够从地图视图“接收消息”,这些方法可以在我链接到的MKMapViewDelegate引用中看到
因此,要实现mapView:didSelectAnnotationView,需要将其添加到.m文件中
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
//if you did all the steps this methosd will be called when a user taps the annotation on the map.
}
发生了什么事
在后台发生的是:
[self.delegate mapView:someMapView didSelectAnnotationView:someAnnotationView];
mapView:didSelectAnnotationView
。MKMapView在其委托上调用该函数。换句话说,当你建立一个MKMapView时,你告诉它:“嘿,听着,任何时候你需要告诉我地图上发生了什么,去告诉这个家伙,他会帮你处理的。”。“guy”是委托对象,它需要实现mapView:didSelectAnnotationView
(这也是它的名称“did select”的原因,即它已经发生了,而不是“select”)。对于一个简单的例子,委托通常是拥有MKMapView的UIViewController
,这就是我将在下面描述的mapView:viewForAnnotation
。因此,如果您发现mapView:didSelectAnnotationView
不是您要查找的内容,请替换此方法UIViewController
扩展而来),以及
*已添加到该视图控制器视图中的MKMapView
,例如名为mapView
要激发的方法定义为MKMapViewDelegate
协议的一部分。
最简单的接线方式是:
- 使UIViewController成为MKMapView的代理
- 在代码中,在
MapViewController.m的
中说,您可以执行viewDidLoad
,或者mapview.delegate=self
- 在Interface Builder中,可以将连接从MKMapView委托属性拖动到文件的所有者
- 在代码中,在
- 然后,在UIViewController上定义一个名为
的方法,在mapView:didSelectAnnotationView
文件中声明它,就像协议一样:MapViewController.m
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view { // whatever you need to do to your annotation and/or map }
祝你好运!天哪,你是某个平行宇宙中的我吗?我发誓,当我点击“发布”时,我看到了新的答案横幅。即使是最后的“祝你好运”,希什。我也会是一个伪君子,不会投上高票!谢谢,我用你的答案和克汉让事情变得令人惊讶。+1感谢你的诚实和好答案。是的,我是你的翻版者(bahhhh)好的帖子解释了提供的答案。在这里,您只编写了没有解释的代码
Place *place = [[Place alloc] init];
PlaceMark *placeMark = [[PlaceMark alloc] initWithPlace:place];
[self.mapView selectAnnotation:placeMark animated:YES];
Place *place = [[Place alloc] init];
PlaceMark *placeMark = [[PlaceMark alloc] initWithPlace:place];
[self.mapView selectAnnotation:placeMark animated:YES];