Search code examples
iosdictionarymkannotationmkannotationview

must implement title when canShowCallout is YES


I'm getting this exception when trying to show the callout on my map annotation, even if the title is set.

This is the init method I'm using in my MapAnnotation class:

- (id)initWithTitle:(NSString *)ttl subtitle:(NSString *)sub andCoordinate:(CLLocationCoordinate2D)c2d {
    titles = ttl;
    coordinate = c2d;
    subtitle = sub;
    return self;
}

Then, somewhere in another class I'm creating the annotations (2 in two different methods):

MapAnnotation *annotation = [[MapAnnotation alloc] initWithTitle:[formatter stringFromDate:sourceDate] subtitle:@"test" andCoordinate:CLLocationCoordinate2DMake(point.latitude, point.longitude)];
[self.mapView addAnnotation:annotation];

And this is the annotations method:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
    if (annotation == mapView.userLocation)
        return nil;

    static NSString *s = @"ann";
    MKAnnotationView *pin = [mapView dequeueReusableAnnotationViewWithIdentifier:s];
    if (!pin) {
        pin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:s];
        pin.canShowCallout = YES;
        pin.image = [UIImage imageNamed:@"pin.png"];
        pin.calloutOffset = CGPointMake(0, 0);
    }
    return pin;
}

Another thing I'd like to do is use two different images for the two map annotations. Any idea?

Thanks.


Solution

  • you MapAnnotation should contain title property, not titles for map callout to work. Declare it as follows

    @property (nonatomic, copy) NSString *title;