views:

2188

answers:

3

Hi all,

I've recently come across this website and I've been trying to add to my call out view a button (from a image).

The code on the websites example works just fine, but when I tried to add in the same code to my project I'm not getting the same results.

There is obviously something I've missed but I cannot seem to work this one out!

Here is my code for annotating:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
    annView.pinColor = MKPinAnnotationColorGreen;

    UIButton *advertButton = [UIButton buttonWithType:UIButtonTypeCustom];
    advertButton.frame = CGRectMake(0, 0, 23, 23);
    advertButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    advertButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

    [advertButton setImage:[UIImage imageNamed:@"button_right.png"] forState:UIControlStateNormal];
    [advertButton addTarget:self action:@selector(showLinks:) forControlEvents:UIControlEventTouchUpInside];

    annView.rightCalloutAccessoryView = advertButton;

    annView.animatesDrop=TRUE;
    annView.canShowCallout = YES;
    annView.calloutOffset = CGPointMake(-5, 5);
    return annView;
}

Any help would be appreciated! :)

+2  A: 

Sorry to answer my own question but this was solved by setting the delegate of the MKMapView to the ViewController. Phew, took me a while to figure that out and it's so simple!!

ing0
+1  A: 

That guy is using a custom image to imitate the detail disclosure. You can actually get the standard detail disclosure quite easily:

[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
Heath Borders
Ah thanks, always useful to do thing how they are meant to be done :)
ing0
A: 

Instead of setting the target like you did here:

[advertButton addTarget:self action:@selector(showLinks:) forControlEvents:UIControlEventTouchUpInside];

You can use the default delegate:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
tul697