Pergunta

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.

Foi útil?

Solução

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];
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top