Cannot call viewForAnnotation function in MapKit

泪湿孤枕 提交于 2019-12-13 19:14:12

问题


I have already checked many questions in StackOverflow but none of them worked for me.

I am developing an iPad application. I created all interfaces in Storyboard.

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.

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 file

@interface CustomMKAnnotation : NSObject <MKAnnotation>
{}

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

Here my viewcontroller class

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 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

I referenced delegate outlet from MKMapView to ViewController. I also set myMap.delegate=self; in MapHolderViewController.m file but it didn't work either.

Any insight is welcome.


回答1:


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

So your method is myMap:viewForAnnotation:.

The correct header is:

- (MKAnnotationView *)mapView:(MKMapView *)mapView 
    viewForAnnotation:(id < MKAnnotation >)annotation

The method name must be 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.

For example, this would be OK:

- (MKAnnotationView *)mapView:(MKMapView *)myMap 
    viewForAnnotation:(id < MKAnnotation >)annotation


来源:https://stackoverflow.com/questions/9919301/cannot-call-viewforannotation-function-in-mapkit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!