[英]Annotation callout view is not responding
我正在嘗試在mapView上顯示注釋。 所有注釋均來自JSON對象。 他們分為三組。 用戶可以通過選擇segmentedIndex控件上的選項來選擇應顯示哪些注釋。
到目前為止,該應用程序正在按預期運行,用戶從segmentedIndex控件中選擇一個選項,並且注釋顯示在mapView上。
我當前的問題是我需要用戶單擊標注視圖以打開另一個viewController。
我認為我的代碼是正確的,但我想並非如此,所顯示的標注視圖是默認的標注視圖,帶有標題和副標題。 單擊任何操作均不會觸發。
歡迎任何幫助。
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *identifier = @"MyLocation";
if ([annotation isKindOfClass:[PlaceMark class]]) {
MKPinAnnotationView *annotationView =
(MKPinAnnotationView *)[myMapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc]
initWithAnnotation:annotation
reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
// Create a UIButton object to add on the
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
[annotationView setRightCalloutAccessoryView:rightButton];
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[leftButton setTitle:annotation.title forState:UIControlStateNormal];
[annotationView setLeftCalloutAccessoryView:leftButton];
return annotationView;
}
return nil;
}
- (void)mapView:(MKMapView *)mapView
annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
if ([(UIButton*)control buttonType] == UIButtonTypeDetailDisclosure){
// Do your thing when the detailDisclosureButton is touched
UIViewController *mapDetailViewController = [[UIViewController alloc] init];
[[self navigationController] pushViewController:mapDetailViewController animated:YES];
} else if([(UIButton*)control buttonType] == UIButtonTypeInfoDark) {
// Do your thing when the infoDarkButton is touched
NSLog(@"infoDarkButton for longitude: %f and latitude: %f",
[(PlaceMark*)[view annotation] coordinate].longitude,
[(PlaceMark*)[view annotation] coordinate].latitude);
}
}
最有可能未設置地圖視圖的delegate
在這種情況下,它將不調用viewForAnnotation
,而是創建一個默認視圖(紅色圖釘,其中的標注僅顯示標題和副標題-沒有按鈕)。
頭文件中的聲明未設置地圖視圖的delegate
。 這只是告訴編譯器該類打算實現某些委托方法。
在xib / storyboard中,右鍵單擊地圖視圖,然后將delegate
出口連接到視圖控制器,或者在viewDidLoad
,放置mapView.delegate = self;
。
無關,但我想指出的是,在calloutAccessoryControlTapped
,而不是檢查buttonType ,您可能只想知道它是右按鈕還是左按鈕,所以只需執行以下操作:
if (control == view.rightCalloutAccessoryView) ...
有關完整的示例,請參見https://stackoverflow.com/a/9113611/467105 。
檢查buttonType
至少有兩個問題:
UIButtonTypeDetailDisclosure
最終實際上會創建一個Info類型的按鈕(有關詳細信息,請參閱MKAnnotationView始終顯示infoButton而不是detailDisclosure btn )。 因此,檢查buttonType UIButtonTypeDetailDisclosure將會失敗(在iOS 7上)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.