0

我有一张地图,并在其中放置了多个注释。当我选择一个注释并点击披露按钮时,会出现一个包含表格视图的新视图(DetailViewController)。我想要的是,当 DetailViewController 打开时,直接选择所选注释的描述。

这是地图和注释,当我标签出现时,显示按钮图像 2 出现,我希望所选注释的描述被选中

在此处输入图像描述

4

3 回答 3

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];
}
于 2012-09-11T12:40:43.780 回答
0

使用此委托方法:

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

并推动你的导航控制器

于 2012-09-11T12:20:59.003 回答
0

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

这会有所帮助。

于 2012-09-11T12:09:31.890 回答