views:

557

answers:

1

Hi all,

Wanted some help with a problem with mapkit I am facing. Should be a silly problem or I have missed out something while going through the mapkit framework.

Here is the senario. I am placing multiple annotation on the map when the user performs some search like pizza. Added button for the right annotation view, on click which opens a next detail view. The problem is how to send some information to the next view, for example I add index to annotations while creating them, now I want to access this information from annotation, and pass it to the next view via the selector set on the button.

I have checked all the mapkit delicate, but don't find a one where I can map this information with the next view and annotation.

Hope I have not confused you guys in my question. Please let me know I will reframe it.

Thaking in advance.

+4  A: 

When you create the UIButton for the annotation, set the tag property (tag is an NSInteger property of UIView) to an id or array index that identifies the relevant object. You can then retrieve that tag value from the sender parameter to your selector.


Edit: here's some sample code.

You create your annotation view and associate the button in your delegate's -mapView:viewForAnnotation: method:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    // Boilerplate pin annotation code
    MKPinAnnotationView *pin = (MKPinAnnotationView *) [self.map dequeueReusableAnnotationViewWithIdentifier: @"restMap"];
    if (pin == nil) {
        pin = [[[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: @"restMap"] autorelease];
    } else {
        pin.annotation = annotation;
    }
    pin.pinColor = MKPinAnnotationColorRed
    pin.canShowCallout = YES;
    pin.animatesDrop = NO;

    // now we'll add the right callout button
    UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    // customize this line to fit the structure of your code.  basically
    // you just need to find an integer value that matches your object in some way:
    // its index in your array of MKAnnotation items, or an id of some sort, etc
    // 
    // here I'll assume you have an annotation array that is a property of the current
    // class and we just want to store the index of this annotation.
    NSInteger annotationValue = [self.annotations indexOfObject:annotation];

    // set the tag property of the button to the index
    detailButton.tag = annotationValue;

    // tell the button what to do when it gets touched
    [detailButton addTarget:self action:@selector(showDetailView:) forControlEvents:UIControlEventTouchUpInside];

    pin.rightCalloutAccessoryView = detailButton;
    return pin;

}

Then in your action method, you'll unpack the value from tag and use it to display the right detail:

-(IBAction)showDetailView:(UIView*)sender {
    // get the tag value from the sender
    NSInteger selectedIndex = sender.tag;
    MyAnnotationObject *selectedObject = [self.annotations objectAtIndex:selectedIndex];

    // now you know which detail view you want to show; the code that follows
    // depends on the structure of your app, but probably looks like:
    MyDetailViewController *detailView = [[MyDetailViewController alloc] initWithNibName...];
    detailView.detailObject = selectedObject;

    [[self navigationController] pushViewController:detailView animated:YES];
    [detailView release];
}
Seamus Campbell
can u plz paste a sample, u r approach looks confusing to me
Ameya
see edit above.
Seamus Campbell
Oops, forgot to add the button to the pin.
Seamus Campbell
cool thanks will check it out
Ameya
Thanks works nicely
Ameya
Thanks for this!
daidai
Although could you please explain the `NSInteger annotationValue = [self.annArray indexOfObject:annotation];` line. This line crashes the app when I use my array of annotations. Is that what you meant? `annArray = [NSArray arrayWithObjects:ann1,ann2,ann3,ann4,nil];[mapView addAnnotations:annArray];` So I would use annArray. Would I need any extra data to the annotation object?
daidai
are you storing `annArray` in an instance variable? The line you are asking about identifies which annotation you're producing a view for so that you can retrieve it later in `showDetailView`. What does the console show after your app crashes?
Seamus Campbell
Yes annArray is an instance variable, which is setup in viewDidLoad where it is added to the map view `[mapView addAnnotations:annArray]` Strangely, if I even try and trace the contents of annArray inside the `mapView:viewForAnnotation:` method, it crashes. Something is definatelty up.The console says nothing cleverly.
daidai
Sounds like you're over-releasing annArray somewhere. I suggest you post a question and show your code.
Seamus Campbell
ok will do thanks
daidai