繁体   English   中英

单击地图注释时显示另一个视图

[英]show another view when map annotation are clicked

我有一个只有一个注释的地图。我创建了一个简单的类,我希望它在用户点击注释时显示。问题是当我点击注释时没有任何反应。

这是我的代码:

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark{
    NSLog(@"Reverse Geocoder completed");
    mPlacemark=placemark;
    [mapView addAnnotation:placemark];
}

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
    annView.animatesDrop=TRUE;

    //create UIButton for annotation
    UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    //NSInteger annotationValue = [self.annotations indexOfObject:annotation];

    [detailButton addTarget:self action:@selector(showDetailView:) forControlEvents:UIControlEventTouchUpInside];
    annView.rightCalloutAccessoryView=detailButton;
    return annView;
}


-(void)showDetailView:(id)sender{
    NSLog("inside the stupid method");
    MyDetailViewController *detailView=[[MyDetailViewController alloc] initWithNibName:@"MyDetailViewController" bundle:nil];
    [[self navigationController] pushViewController:detailView animated:YES];
    [detailView release];
}

我的showDetailView函数永远不会被调用。请帮助我。我是iphone的新手,我可能会忘记一件简单的事情。谢谢

还是行不通!!!!

首先,检查是否设置了地图视图的delegate ,否则将不会调用viewForAnnotation方法。

接下来,附件按钮出现在注释的标注上 ,只有在设置了canShowCallout才会出现:

annView.canShowCallout = YES;

接下来,使用地图视图自己的calloutAccessoryControlTapped委托方法,而不是使用自己的方法来处理按钮操作,这种方法在点击附件时会被调用:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    NSLog(@"inside the stupid method");

    //Here, the annotation tapped can be accessed using view.annotation
}

viewForAnnotation删除[detailButton addTarget...行。

另请注意,showDetailView方法中的NSLog缺少前导@ ,这会在调用方法时导致崩溃。

另一件事是你应该在viewForAnnotation使用dequeueReusableAnnotationViewWithIdentifier来重新使用注释视图。


请求示例

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
    static NSString *annReuseId = @"currentloc";

    MKPinAnnotationView *annView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annReuseId];
    if (annView == nil)
    {
        annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annReuseId];

        annView.animatesDrop = YES;
        annView.canShowCallout = YES;

        UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        annView.rightCalloutAccessoryView=detailButton;
    }
    else {
        annView.annotation = annotation;
    }

    return annView;
}

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    NSLog(@"calloutAccessoryControlTapped: annotation = %@", view.annotation);
    MyDetailViewController *detailView=[[MyDetailViewController alloc] initWithNibName:@"MyDetailViewController" bundle:nil];
    //here, can set annotation info in some property of detailView
    [[self navigationController] pushViewController:detailView animated:YES];
    [detailView release];
}

好。 如果您使用的是故事板,则需要在地图视图控制器之间创建一个segue,然后在下面的代码所在的位置创建一个segue:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    NSLog(@"calloutAccessoryControlTapped: annotation = %@", view.annotation);
    MyDetailViewController *detailView=[[MyDetailViewController alloc] initWithNibName:@"MyDetailViewController" bundle:nil];
    //here, can set annotation info in some property of detailView
    [[self navigationController] pushViewController:detailView animated:YES];
    [detailView release];
}

请改用以下代码:

NSLog(@"calloutAccessoryControlTapped: annotation = %@", view.annotation);
[self performSegueWithIdentifier:@"DetailViewController" sender:self];

这将为您提供详细视图的移动。

像这样编写你的函数:

-(IBAction)showDetailView:(id)sender{
    NSLog("inside the stupid method");
    MyDetailViewController *detailView=[[MyDetailViewController alloc] initWithNibName:@"MyDetailViewController" bundle:nil];
    [[self navigationController] pushViewController:detailView animated:YES];
    [detailView release];
}

替换-(IBAction)showDetailView:(UIView*)sender具有以下内容......

    -(void)showDetailView:(id)sender

你必须像这样设置地图委托给自己,

mapview.delegate=self; (如果你在同一页面调用方法)

如果您没有在代码中分配代理,则不会调用相应的方法,因此请检查您是否具有上述行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM