i have a map and multiple annotations are dropped at. when i select an annotation, and i tap the disclosure button a new view appears (DetailViewController) that contains a table view. What do i want is, when the DetailViewController opens, directly the description of the selected annotation become selected.
Asked
Active
Viewed 4,121 times
3 Answers
1
You can use the below code for detecting when MKAnnotation is selected in MKMapView
@interface ClassVC ()
{
int notationTag;
}
- (void)viewDidLoad
{
notationTag = 0; //initialisation of variable
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
// If it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
// Handle any custom annotations.
if ([annotation isKindOfClass:[MKPointAnnotation class]])
{
// Try to dequeue an existing pin view first.
MKAnnotationView *pinView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];
if (!pinView)
{
// If an existing pin view was not available, create one.
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:@"mallicon.png"];
pinView.calloutOffset = CGPointMake(0, 32);
//pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinView.tag = notationTag;
} else {
pinView.annotation = annotation;
}
notationTag++;
return pinView;
}
return nil;
}
then in calloutAccessoryControlTapped.,
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
ContactDetails *contact=[[ContactDetails alloc] initWithNibName:@"ContactDetails" bundle:nil];
contact.name1Str = [NSString stringWithFormat:@"%d",view.tag];
}
in the next view add this line of code
NSIndexPath *index =[NSIndexPath indexPathWithIndex:[name1Str intValue]];
[tableView selectRowAtIndexPath:index animated:NO scrollPosition:UITableViewScrollPositionMiddle];
In my case I used below code to reload you can try that too
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
self.mapView.centerCoordinate = view.annotation.coordinate;
NSLog(@"======Tag====%ld", (long)view.tag);
NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:[view tag] inSection:0];
[self.collectionView scrollToItemAtIndexPath:rowToReload atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
}

Mahendra Vishwakarma
- 486
- 3
- 14

Manikandan
- 321
- 2
- 12
-
Good answer it saves my time pinView.tag = i; this code was awesome – Nischal Hada Dec 23 '15 at 18:20
0
use the delegate method didSelectAnnotationView:
and push the detalViewController into your navigation controller
This will help.

Pranjal Bikash Das
- 1,092
- 9
- 27
-
i did that, but how to let the row that contains the details of the selected annotation become selected?! – fadd Sep 11 '12 at 12:32
-
you can do it by many ways.. All you need to do is set serial numbers to every annotations and pass that serial no to the detailViewController. For setting a row selected, following link will help http://stackoverflow.com/questions/1323474/how-do-i-select-a-uitableviewcell-by-default – Pranjal Bikash Das Sep 11 '12 at 12:43
-
yes dude, i gave each pin a serial number already. but how to detect with one i have selected?? – fadd Sep 12 '12 at 07:59
0
Use this delegate method:
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
AnnotationView *annotation = (AnnotationView *)mapView.annotation;
NSInteger *yourIndex = annotation.myindex;
}
and push your navigation controller

Hemant Dixit
- 1,145
- 8
- 12
-
i did that, but how to let the row that contains the details of the selected annotation become selected?! – fadd Sep 11 '12 at 12:32