How to call pin annotation on button click in iPhone

我只是一个虾纸丫 提交于 2019-12-02 10:30:04

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/6058939/how-to-call-pin-annotation-on-button-click-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!