【发布时间】:2014-03-17 02:59:49
【问题描述】:
我正在尝试在 mapView 上显示注释。所有注释都来自 JSON 对象。他们分为三组。用户可以通过选择 segmentedIndex 控件上的选项来选择应显示哪些注释。
就目前而言,应用程序按预期运行,用户从 segmentedIndex 控件中选择一个选项,并且注释显示在 mapView 上。
我当前的问题是我需要用户单击标注视图以打开另一个视图控制器。
我认为我的代码是正确的,但我想它不是这样显示的标注视图是默认标注视图,带有标题和副标题。单击它时不会触发任何操作。
欢迎任何帮助。
- (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。如果设置了委托,请在设置辅助按钮的行上方或下方放置断点或 NSLog,并确保代码正在执行。 -
不相关但是:在
calloutAccessoryControlTapped中,您可能只想知道它是 right 还是 ,而不是检查 buttonType左按钮,所以只需执行if (control == view.rightCalloutAccessoryView)等。请参阅stackoverflow.com/a/9113611/467105以获取示例。 -
@Anna,我在头文件中有以下声明: interface mapKitViewController : UIViewController
,你的意思是我已经设置了地图视图的委托吗?或者你的意思是我应该把它放在另一个地方? -
头文件中的声明没有设置地图视图的委托。这只是告诉编译器这个类打算实现某些委托方法。在 xib/storyboard 中,右键单击地图视图并将代理连接到视图控制器或在 viewDidLoad 中输入
mapView.delegate = self;。 -
谢谢@Anna,我已经更改了我所有的 viewController 代码以使其更清晰。如果你不介意,我希望你把你的 cmets 放在一个答案中,让我接受它。
标签: ios mapkit mkannotationview