我使用此代码创建我的自定义注释 View 。
- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation {
MKPinAnnotationView *pinAnnotation = nil;
if(annotation != myMapView.userLocation) {
static NSString *defaultPinID = @"myPin";
pinAnnotation = (MKPinAnnotationView *)[myMapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinAnnotation == nil )
pinAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID];
pinAnnotation.canShowCallout = YES;
//instatiate a detail-disclosure button and set it to appear on right side of annotation
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[infoButton addTarget:self
action:@selector(showRestaurantDescription)
forControlEvents:UIControlEventTouchUpInside];
pinAnnotation.rightCalloutAccessoryView = infoButton;
}
return pinAnnotation;
}
我的问题是如何将参数发送到我的选择器:-showRestaurantDescription:
我认为我可以向按钮添加标签并处理它,但我需要选择最佳方式,因为我的餐厅有数组,当我点击具体的图钉时,我需要显示当前餐厅。所以在这种情况下,标签并不像我想的那样方便。
最佳答案
您应该使用委托(delegate)方法 mapView:annotationView:calloutAccessoryControlTapped:
来监听按钮上的点击。该方法传递给一个 annotationView
,它有一个 annotation
属性。您可以使用注释属性找出它是哪家餐厅。
此外,您是否忘记了 action:@selector(showRestaurantDescription)
中的冒号?
关于ios - MKAnnotationView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11685875/