检测选定的Map Annotation Xcode [英] detecting the selected Map Annotation Xcode

查看:101
本文介绍了检测选定的Map Annotation Xcode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

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

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

然后在calloutAccessoryControlTapped。,

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

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

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

这篇关于检测选定的Map Annotation Xcode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆