如何在iPhone中单击按钮时调用图钉注释 [英] How to call pin annotation on button click in iPhone

查看:60
本文介绍了如何在iPhone中单击按钮时调用图钉注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

您尝试调用的方法是mapView委托方法,并且只能由MapView调用.此方法仅用于在mapView需要时按需提供注释的视图. IE.当注释进入或即将移入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.

您需要将注释对象添加到mapView中.该对象必须符合MKAnnotation协议. http://developer.apple.com/library/ios/documentation/MapKit/参考/MKAnnotation_Protocol/

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/

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

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.

这篇关于如何在iPhone中单击按钮时调用图钉注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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