无法在MapKit中调用viewForAnnotation函数 [英] Cannot call viewForAnnotation function in MapKit
问题描述
我已经在StackOverflow中检查了很多问题,但它们都没有为我工作。
I have already checked many questions in StackOverflow but none of them worked for me.
我正在开发一个iPad应用程序。我在Storyboard中创建了所有接口。
I am developing an iPad application. I created all interfaces in Storyboard.
对于我的项目,我创建了一个视图控制器和相应的.h .m文件并将它们链接起来。然后我在视图控制器中添加了MKMapView,然后使用Interface Builder在视图控制器文件中引用了mapView。我创建了一堆点并在地图上显示它们。
For my project I created one view controller and corresponding .h .m files and linked them. Then I added MKMapView inside of the view controller then I referenced mapView in view controller file by using Interface Builder. I created bunch of points and showed them on map.
在我决定改变引脚颜色之前,一切都很完美。我在Apple Developer网站上关注了MapCallout示例。然后我创建了自定义注释点类,如下所示
Everything was perfect until I decided to change pin colors. I followed MapCallout example from Apple Developer site. Then I Created custom annotation point class as you see below
CustomMKAnnotation.h文件
CustomMKAnnotation.h file
@interface CustomMKAnnotation : NSObject <MKAnnotation>
{}
CustomMKAnnotation.m文件
CustomMKAnnotation.m file
@implementation CustomMKAnnotation
- (NSString *)title {return @"title";}
- (NSString *)subtitle{return @"subtitle";}
- (CLLocationCoordinate2D)coordinate;{
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude = some latitude;
theCoordinate.longitude = some longitude;
return theCoordinate;
}
@end
这里是我的viewcontroller类
Here my viewcontroller class
MapHolderViewController.h文件
MapHolderViewController.h file
@interface MapHolderViewController : UIViewController<MKMapViewDelegate>
{
NSMutableArray *mapAnnotations;
}
@property (nonatomic,retain) IBOutlet MKMapView *myMap;
@property (nonatomic, retain) NSMutableArray *mapAnnotations;
-(void) addLatLong;
@end
MapHolderViewController.m文件
MapHolderViewController.m file
@implementation MapHolderViewController
@synthesize myMap;
@synthesize mapAnnotations;
-(void) addLatLong{
// I am adding annotations here
}
- (MKAnnotationView *)myMap:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
// This function has never been called.
}
@end
我将MKMapView的委托出口引用到ViewController。我还设置了myMap.delegate = self;在MapHolderViewController.m文件中,但它也无法正常工作。
I referenced delegate outlet from MKMapView to ViewController. I also set myMap.delegate=self; in MapHolderViewController.m file but it didn't work either.
欢迎任何见解。
推荐答案
问题似乎是你的委托方法没有正确命名。
The problem seems to be that your delegate method is not named correctly.
你已经编写了这样的委托方法:
You have written the delegate method like this:
- (MKAnnotationView *)myMap:(MKMapView *)mapView
viewForAnnotation:(id <MKAnnotation>)annotation
所以你的方法是 myMap:viewForAnnotation:
。
正确的标题是:
- (MKAnnotationView *)mapView:(MKMapView *)mapView
viewForAnnotation:(id < MKAnnotation >)annotation
方法名称必须 mapView:viewForAnnotation:
。
您可以更改参数名称(参数类型后面的名称)但不是参数类型前面的部分,它们是方法名称的一部分。
You can change the parameter names (the names after the parameter types) but not the parts in front of the parameter types which are part of the method name.
例如,这样就可以了:
- (MKAnnotationView *)mapView:(MKMapView *)myMap
viewForAnnotation:(id < MKAnnotation >)annotation
这篇关于无法在MapKit中调用viewForAnnotation函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!