Ios 注释右附件按钮赢得';不变
我的注释触发正确的操作,但无论我选择哪个按钮,它们都不会显示正确的附件按钮。下面是我的代码Ios 注释右附件按钮赢得';不变,ios,objective-c,mapkit,Ios,Objective C,Mapkit,我的注释触发正确的操作,但无论我选择哪个按钮,它们都不会显示正确的附件按钮。下面是我的代码 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{ static NSString *s = @"ann"; MKAnnotationView *pin = [mapView dequeueReusableAnnotationVi
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
static NSString *s = @"ann";
MKAnnotationView *pin = [mapView dequeueReusableAnnotationViewWithIdentifier:s];
if (annotation != self.mapView.userLocation) {
pin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:s];
pin.canShowCallout = YES;
pin.calloutOffset = CGPointMake(0, 0);
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pin.rightCalloutAccessoryView = button;
[button addTarget:self
action:@selector(getDirections:)
forControlEvents:UIControlEventTouchUpInside];
}
return pin;
}
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释{
静态NSString*s=@“ann”;
MKAnnotationView*引脚=[mapView dequeueReusableAnnotationViewWithIdentifier:s];
if(注释!=self.mapView.userLocation){
pin=[[MKPinAnnotationView alloc]initWithAnnotation:annotation重用标识符:s];
pin.canShowCallout=是;
pin.calloutOffset=CGPointMake(0,0);
UIButton*button=[UIButton button类型:UIButtonTypedTailButton];
pin.rightCalloutAccessoryView=按钮;
[按钮添加目标:自我
操作:@选择器(getDirections:)
forControlEvents:UIControlEventTouchUpInside];
}
回位销;
}
试试这个:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
// If it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
// Handle any custom annotations.
MKAnnotationView *pin = [mapView dequeueReusableAnnotationViewWithIdentifier:@"ann"];
if(pin){
pin.annotation = annotation;
} else {
pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"ann"];
}
pin.canShowCallout = YES;
pin.calloutOffset = CGPointMake(0, 0);
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pin.rightCalloutAccessoryView = button;
[button addTarget:self
action:@selector(getDirections:)
forControlEvents:UIControlEventTouchUpInside];
return pin;
}
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释{
//如果是用户位置,只需返回nil。
if([annotation isKindOfClass:[MKUserLocation类]])
返回零;
//处理任何自定义注释。
MKAnnotationView*引脚=[mapView dequeueReusableAnnotationViewWithIdentifier:@“ann”];
如果(pin){
pin.annotation=注释;
}否则{
pin=[[MKPinAnnotationView alloc]initWithAnnotation:annotation重用标识符:@“ann”];
}
pin.canShowCallout=是;
pin.calloutOffset=CGPointMake(0,0);
UIButton*button=[UIButton button类型:UIButtonTypedTailButton];
pin.rightCalloutAccessoryView=按钮;
[按钮添加目标:自我
操作:@选择器(getDirections:)
forControlEvents:UIControlEventTouchUpInside];
回位销;
}
尝试将MKAnnotationView更改为MKPinAnnotationView,如果“注释触发正确的操作”是什么意思?是否设置了地图视图的委托?您是否确认您的viewForAnnotation委托方法实际上正在被调用(在其中放置断点)?user22。。这给了我一个错误:不兼容的指针类型正在使用类型为“MKAnnotationView*”的表达式初始化“MKPinAnnotationView*”@安娜,我的意思是点击注释有一个动作,这个动作是有效的。正在调用委托方法。行“pin=annotation”给了我以下警告:从不兼容的类型“\u strong id”分配给“MKAnnotationView*\u strong”,很抱歉我弄错了,它应该是pin.annotation=annotation;不幸的是,我仍然没有看到按钮。这里的旁注:您不需要将操作添加到正在创建的按钮中。“如果您指定的视图也是UIControl
类的后代,您可以在点击控件时使用地图视图的委托来接收通知。如果它不是从UIControl
,则您的视图负责处理其边界内的任何触摸事件。”请参阅-[MKMapViewDelegate-mapView:annotationView:CalloutAccessoryControl:::
。