0

I'm working with a MapView and have 30 pin locations placed and everything's working great. I added a button in the callout with a rightCalloutAccessoryView. Here's the button code:

UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    pinView.rightCalloutAccessoryView = rightButton;
    [rightButton addTarget:self action:@selector(rightButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    return pinView;
}

This works fine and calls the "rightButtonPressed" method. Here's the implementation of that method:

-(IBAction) rightButtonPressed:(id)sender
{
    NSLog(@"button clicked");
    MapViewController *thisMap = (MapViewController *)[[UIApplication sharedApplication] delegate];
    DetailViewController *dvc = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil];
    [thisMap switchViews:self.view toView:dvc.view];
}

So, as you see, any time I touch any button from all 30 pin callouts, it goes to the same view (DetailViewController). I want each button to have it's own view to switch to (it's where I'm going to place "Directions To Here" and "Directions From Here" etc. as well as the store address and name).

I realize that I could make 30 views and make 30 different methods that could apply to each pin but I know there has to be a more concise code that deals with arrays.

Possible certain arrays could be called in a UILabel on the DetailViewController so it will just load the appropriate information and also give directions to the appropriate location.

This might be a big question and I've looked around for some tutorials but haven't found anything that answers the question exactly. If anyone could get me started (or even point me in the right direction), I'd be totally grateful.

MillerMedia
  • 3,651
  • 17
  • 71
  • 150

1 Answers1

1

With annotation view callout accessories, it's much better to use the map view's own delegate method calloutAccessoryControlTapped to handle the button press instead of using addTarget and your own custom method.

In the calloutAccessoryControlTapped delegate method, you can directly access the annotation that was tapped using view.annotation without having to figure out which annotation in an array or whatever data structure was tapped.

Remove the call to addTarget and replace the rightButtonPressed: method with:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
            calloutAccessoryControlTapped:(UIControl *)control
{
    //cast the plain view.annotation to your custom class so you can
    //easily access your custom annotation class' properties...
    YourAnnotationClass *annotationTapped = (YourAnnotationClass *)view.annotation;

    NSLog(@"button clicked on annotation %@", annotationTapped);

    MapViewController *thisMap = (MapViewController *)[[UIApplication sharedApplication] delegate];
    DetailViewController *dvc = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil];

    //annotationTapped can be passed to the DetailViewController
    //or just the properties needed can be passed...
    //Example line below assumes you add a annotation property to DetailViewController.
    dvc.annotation = annotationTapped;

    [thisMap switchViews:self.view toView:dvc.view];
}
  • Great! That's much easier than I thought (I'm still getting myself familiar with the map view delegate's methods). The one issue I'm having now (which I had with my previous method too) is that the program and everything works fine but on the line '[thisMap switchViews:self.view toView:dvc.view];' it gives me the warning that ''MapViewController' may not respond to 'switchViews:toView:' even though it does. Just want to clear it up now in case it becomes an issue later. Thanks so much! – MillerMedia Oct 14 '11 at 17:18
  • Make sure you have declared the switchViews method in the MapViewController.h (interface) file. –  Oct 14 '11 at 17:24
  • I'm confused since the switchViews method is called within the '-(void)mapView' method, what's the syntax to declare it? I know how to declare a method on it's own but what about something like this? (Sorry! I'm still learning...) – MillerMedia Oct 14 '11 at 17:36
  • 1
    It doesn't matter where it's called from. The warning is saying MapViewController has not declared such a method so calling it may result in a crash at runtime. In MapViewController.m, you have the switchViews method. Just take the method heading (eg. `-(void)switchViews:(UIView *)fromView toView:(UIView *)toView;`) and add it to the MapViewController.h file before the @end. –  Oct 14 '11 at 17:45