iphone - 单击 map 注释时显示另一个 View

标签 iphone dictionary annotations

我有一张只有一个注释的 map 。我创建了一个简单的类,我希望它在用户单击注释时显示。问题是,当我单击注释时什么也没有发生。

这是我的代码:

- (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 新手,我可能会忘记一件简单的事情。谢谢

还是不行!!!

最佳答案

首先,检查 map View 的 delegate 是否已设置,否则您的 viewForAnnotation 方法将不会被调用。

接下来,附件按钮将出现在注释的标注上,仅当您设置canShowCallout时才会出现:

annView.canShowCallout = YES;

接下来,不要使用您自己的方法来处理按钮操作,最好使用 map View 自己的 calloutAccessoryControlTapped 委托(delegate)方法,该方法在点击附件时会被调用:

- (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 来启用注释 View 重用。


请求的示例:

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

关于iphone - 单击 map 注释时显示另一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7334068/

相关文章:

python - 构建所选特征的字典

c++ - 如何将矩阵映射到某个整数值?

java - 如何为 lombok 编写具有单一注释的集合数据类型的自定义 build() 函数?

iphone - 有没有办法使用 iPhone SDK 将 iPod 音乐文件保存到 iPhone 应用程序?

java - Jackson JSON解析映射转换

java - 如何打印Java标记注释?

java - 如何使用 Micronaut 客户端注释映射 errorType

iphone - 使用 SMTPSender/SKPSMTPMessage 发送电子邮件给出错误 -[NSConcreteMutableDataencodeBase64ForData] : unrecognized selector sent to instance 0x6a67190

iphone - 键盘显示后 TableView 内容高度已更改

ios - 升级到 iOS9 后登录流程失败