簡體   English   中英

如何在iPhone中單擊按鈕時調用圖釘注釋

[英]How to call pin annotation on button click in iPhone

我不希望在地圖上直接調用圖釘,我希望在按鈕操作上調用圖釘注釋。

當我在按鈕單擊事件上調用此方法時,我的應用程序崩潰了。 我想在按鈕單擊時調用注釋。 我可以在按鈕上調用所有方法嗎?

- (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需要時按需提供注釋的視圖。 即當注釋位於或即將移入mapView的可見區域時。

您需要在mapView中添加一個注釋對象。 該對象必須符合MKAnnotation協議。 http://developer.apple.com/library/ios/documentation/MapKit/Reference/MKAnnotation_Protocol/

您將通過在按下按鈕時調用的IBAction方法中調用[yourMapView addAnnotation:yourAnnotationObject],將對象添加到mapView中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM