9

I have a map with only one annotation.I created a simple class which I want it to show when the user clicks on the annotation.The problem is that when I click on the annotation nothing happens.

Here is my code:

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark{
    NSLog(@"Reverse Geocoder completed");
    mPlacemark=placemark;
    [mapView addAnnotation:placemark];
}

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

    //create UIButton for annotation
    UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    //NSInteger annotationValue = [self.annotations indexOfObject:annotation];

    [detailButton addTarget:self action:@selector(showDetailView:) forControlEvents:UIControlEventTouchUpInside];
    annView.rightCalloutAccessoryView=detailButton;
    return annView;
}


-(void)showDetailView:(id)sender{
    NSLog("inside the stupid method");
    MyDetailViewController *detailView=[[MyDetailViewController alloc] initWithNibName:@"MyDetailViewController" bundle:nil];
    [[self navigationController] pushViewController:detailView animated:YES];
    [detailView release];
}

My showDetailView function never gets called.Please help me.I'm new to iphone and I might forget a simple thing.Thanks

Still not working!!!!

adrian
  • 4,574
  • 17
  • 68
  • 119

5 Answers5

16

First, check that the map view's delegate is set otherwise your viewForAnnotation method will not get called.

Next, the accessory button appears on the annotation's callout which will only appear if you set canShowCallout:

annView.canShowCallout = YES;

Next, instead of using your own method to handle the button action, it's much better to use the map view's own calloutAccessoryControlTapped delegate method which gets called when tapping an accessory:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    NSLog(@"inside the stupid method");

    //Here, the annotation tapped can be accessed using view.annotation
}

Remove the [detailButton addTarget... line from viewForAnnotation.

Also note your NSLog in the showDetailView method is missing the leading @ which will result in a crash when the method does get called.

Another thing is you should use dequeueReusableAnnotationViewWithIdentifier in viewForAnnotation to enable annotation view re-use.


Example as requested:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
    static NSString *annReuseId = @"currentloc";

    MKPinAnnotationView *annView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annReuseId];
    if (annView == nil)
    {
        annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annReuseId];

        annView.animatesDrop = YES;
        annView.canShowCallout = YES;

        UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        annView.rightCalloutAccessoryView=detailButton;
    }
    else {
        annView.annotation = annotation;
    }

    return annView;
}

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    NSLog(@"calloutAccessoryControlTapped: annotation = %@", view.annotation);
    MyDetailViewController *detailView=[[MyDetailViewController alloc] initWithNibName:@"MyDetailViewController" bundle:nil];
    //here, can set annotation info in some property of detailView
    [[self navigationController] pushViewController:detailView animated:YES];
    [detailView release];
}
  • is great what you are saying but could u post a complete code, please? – adrian Sep 07 '11 at 13:19
  • I run into a similar problem, the NSLog show the String so the `callOutAccessoryControlTapped` method gets called, but nothing is happened, the detailed view isn't showed, i work with StoryBoards – Malloc Jan 06 '12 at 13:41
  • @Malek, I haven't used storyboards yet so can't give specific advice. You should ask that as a new question. If you are referring to your latest question, you should update it and mention you are using storyboards. –  Jan 06 '12 at 13:48
  • callOutAccessoryControlTapped called but after CalloutView disappeared... weird behavior. work fine in test project. – bLacK hoLE Dec 01 '16 at 15:11
2

Ok. If you are using storyboards, you need to create a segue between the map view controller and then where the below code exists:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    NSLog(@"calloutAccessoryControlTapped: annotation = %@", view.annotation);
    MyDetailViewController *detailView=[[MyDetailViewController alloc] initWithNibName:@"MyDetailViewController" bundle:nil];
    //here, can set annotation info in some property of detailView
    [[self navigationController] pushViewController:detailView animated:YES];
    [detailView release];
}

Use the following code instead:

NSLog(@"calloutAccessoryControlTapped: annotation = %@", view.annotation);
[self performSegueWithIdentifier:@"DetailViewController" sender:self];

This will give you the movement to the detailed view.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Stan Cromlish
  • 483
  • 5
  • 10
0

write your function like this:

-(IBAction)showDetailView:(id)sender{
    NSLog("inside the stupid method");
    MyDetailViewController *detailView=[[MyDetailViewController alloc] initWithNibName:@"MyDetailViewController" bundle:nil];
    [[self navigationController] pushViewController:detailView animated:YES];
    [detailView release];
}
progrmr
  • 75,956
  • 16
  • 112
  • 147
Rahul Juyal
  • 2,124
  • 1
  • 16
  • 33
0

Replace -(IBAction)showDetailView:(UIView*)sender with following...

    -(void)showDetailView:(id)sender
Adrian P
  • 6,479
  • 4
  • 38
  • 55
alloc_iNit
  • 5,173
  • 2
  • 26
  • 54
0

you have to set the map delegate to self like this,

mapview.delegate=self; (if your calling the method in same page)

if you don't assign the delegate in your code the respective methods won't be called, so check if you have the above mentioned line.

EdChum
  • 376,765
  • 198
  • 813
  • 562
Santosh
  • 9
  • 3