【问题标题】:detecting the selected Map Annotation Xcode检测选定的地图注释 Xcode
【发布时间】:2012-09-11 12:05:39
【问题描述】:

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

【问题讨论】:

    标签: iphone objective-c ios xcode ipad


    【解决方案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];
    }
    

    【讨论】:

    • 好答案,它节省了我的时间 pinView.tag = i;这段代码很棒
    【解决方案2】:

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

    这会有所帮助。

    【讨论】:

    • 我做到了,但是如何让包含所选注释详细信息的行被选中?!
    • 您可以通过多种方式做到这一点。您需要做的就是为每个注释设置序列号并将该序列号传递给 detailViewController。要设置选中的行,以下链接将帮助stackoverflow.com/questions/1323474/…
    • 是的,伙计,我已经给每个别针一个序列号了。但是如何检测我选择的一个??
    【解决方案3】:

    使用这个委托方法:

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

    然后推送你的导航控制器

    【讨论】:

    • 我做到了,但是如何让包含所选注释详细信息的行被选中?!
    猜你喜欢
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    • 2018-05-10
    • 1970-01-01
    • 2021-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多