如何在 iPhone 中单击按钮时调用 pin 注释

发布于 2024-11-08 18:27:42 字数 1038 浏览 0 评论 0原文

我不希望直接在地图上调用图钉,我希望按钮操作调用图钉注释。

当我在按钮单击事件上调用此方法时,我的应用程序崩溃了。我想在单击按钮时调用注释。我可以在按钮上调用所有方法吗?

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    MKPinAnnotationView *pin = (MKPinAnnotationView *) [self.map dequeueReusableAnnotationViewWithIdentifier: @"restMap"];
    if (pin == nil) {
        pin = [[[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: @"restMap"] autorelease];
    } else {
        pin.annotation = annotation;
    }
    pin.pinColor = MKPinAnnotationColorRed
    pin.canShowCallout = YES;
    pin.animatesDrop = NO;
    UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    NSInteger annotationValue = [self.annotations indexOfObject:annotation];
    detailButton.tag = annotationValue;
    [detailButton addTarget:self action:@selector(showDetailView:) forControlEvents:UIControlEventTouchUpInside];
    pin.rightCalloutAccessoryView = detailButton;
    return pin;
}  

I dont want pin to be call directly on map, I want this on button action to call pin annotation.

When I call this method on button click event my app was crash. I want to call annotation on button click. Can I call this all method on button?

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    MKPinAnnotationView *pin = (MKPinAnnotationView *) [self.map dequeueReusableAnnotationViewWithIdentifier: @"restMap"];
    if (pin == nil) {
        pin = [[[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: @"restMap"] autorelease];
    } else {
        pin.annotation = annotation;
    }
    pin.pinColor = MKPinAnnotationColorRed
    pin.canShowCallout = YES;
    pin.animatesDrop = NO;
    UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    NSInteger annotationValue = [self.annotations indexOfObject:annotation];
    detailButton.tag = annotationValue;
    [detailButton addTarget:self action:@selector(showDetailView:) forControlEvents:UIControlEventTouchUpInside];
    pin.rightCalloutAccessoryView = detailButton;
    return pin;
}  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白云不回头 2024-11-15 18:27:42

您尝试调用的方法是 MapView 委托方法,并且只能由您的 MapView 调用。此方法仅用于在 mapView 需要时“按需”提供注释的视图。即当注释位于或即将移入mapView 的可见区域时。

您需要将注释对象添加到您的地图视图中。该对象必须符合 MKAnnotation 协议。
http://developer.apple.com/library/ios/documentation/ MapKit/Reference/MKAnnotation_Protocol/

您将通过在按下按钮时调用的 IBAction 方法中调用 [yourMapView addAnnotation: yourAnnotationObject] 将对象添加到 mapView 中。

The method that you are trying to call is a mapView delegate method and should only be called by your MapView. This method is only for providing the annotation's view "on demand" when the mapView needs it. I.e. When the annotation is in or about to move into the mapView's visible region.

You need to add an annotation object to your mapView. This object must conform to the MKAnnotation Protocol.
http://developer.apple.com/library/ios/documentation/MapKit/Reference/MKAnnotation_Protocol/

You will add the object to the mapView by calling [yourMapView addAnnotation: yourAnnotationObject] in the IBAction method that is called when your button is pushed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文