检测选定的Map Annotation Xcode

时间:2012-09-11 12:05:39

标签: iphone objective-c ios xcode ipad

我有一张地图,并删除了多个注释。当我选择一个注释,然后点击显示按钮时,会出现一个包含表格视图的新视图(DetailViewController)。 我想要的是,当DetailViewController打开时,直接选择的注释的描述被选中。

this is the map and the annotations, when i tab the appeared the disclosure button image 2 appears and i want the description of the selected annotation become selected

enter image description here

3 个答案:

答案 0 :(得分:1)

您可以使用以下代码检测MKMapView中选择MKAnnotation的时间

@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;
}

然后在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];
}

在下一个视图中添加此行代码

NSIndexPath *index =[NSIndexPath indexPathWithIndex:[name1Str intValue]];
[tableView selectRowAtIndexPath:index animated:NO scrollPosition:UITableViewScrollPositionMiddle];

在我的情况下,我使用下面的代码重新加载你也可以尝试

- (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];
}

答案 1 :(得分:0)

使用委托方法didSelectAnnotationView:并将detalViewController推送到导航控制器

这会有所帮助。

答案 2 :(得分:0)

使用此委托方法:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
      AnnotationView *annotation = (AnnotationView *)mapView.annotation;
      NSInteger *yourIndex = annotation.myindex;
}

并推送导航控制器