I created a subclass of MKAnnotationView
which when selected display a callout view (similar to the MKPinAnnotationView) which is just a subclass of UIView
with a button and text for now. The callout is added to the annotation view. The problem i have is that the button i put in the callout view never trigger any action, it does not seem to respond to touch event.
The reason i create a new MKAnnotationView
is because i need to display more
than text and two buttons in the annotation view and i could not find a way
to achieve this with the regular MKPinAnnotationView
callouts.
Code:
- (MKAnnotationView *) mapView:(MKMapView *) mapView viewForAnnotation:(id ) annotation {
if(![annotation isKindOfClass:[CSMapAnnotation class]])
return nil;
CSMapAnnotation* csAnnotation = (CSMapAnnotation*)annotation;
CustomAnnotation *customAnnotationView=[[[CustomAnnotation alloc] initWithAnnotation:annotation reuseIdentifier:nil] autorelease];
MKPinAnnotationView* pin = nil; pin = [[[MKPinAnnotationView alloc] initWithAnnotation:customAnnotationView reuseIdentifier:@"identifier"] autorelease];
[pin setPinColor: MKPinAnnotationColorRed ];
MKAnnotationView *annotationView = pin;
annotationView.canShowCallout = YES;
[pin setPinColor:(csAnnotation.annotationType == CSMapAnnotationTypeStart) ? MKPinAnnotationColorGreen : ((csAnnotation.annotationType == CSMapAnnotationTypeCarLocation)? MKPinAnnotationColorPurple:MKPinAnnotationColorRed) ];
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0,0,85,25)];
[leftView setBackgroundColor:[UIColor clearColor]];
UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
[shareButton setBackgroundImage:[UIImage imageNamed:@"share.png"] forState:UIControlStateNormal];
[shareButton setFrame:CGRectMake(19+GAP, 0, 25, 25)];
[shareButton addTarget:self action:@selector(shareButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[leftView addSubview:shareButton];
UIButton *setAlertButton = [UIButton buttonWithType:UIButtonTypeCustom];
[setAlertButton setBackgroundImage:[UIImage imageNamed:@"alert.png"] forState:UIControlStateNormal];
[setAlertButton setFrame:CGRectMake(shareButton.frame.origin.x+shareButton.frame.size.width+GAP, 0, 25, 25)];
[setAlertButton addTarget:self action:@selector(alertButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[leftView addSubview:setAlertButton];
annotationView.rightCalloutAccessoryView = leftView;
[leftView release];
return annotationView;
}
-(void)shareButtonAction:(id)sender {
NSLog(@"%s",_cmd);
}
The shareButtonAction ac开发者_JAVA百科tion is not calling when I am clicking the shareButton...
I checked above delegate also but It is not called when installed to iPhone 3.0.. This delegate will call if i have only one button to right callout but (In the above case)it is not calling when adding view with two buttons. Its happening in iPhone 3.0.
I don't know if you are aware (or if you care) but you are passing an MKAnnotationView to a method that asks for MKAnnotation and this is bound to cause you problems later down the track.
Also because of that, I don't know if my answer will actually work for you but there is a delegate method that gels called when you tap the callout's accessory control. After setting yourself as the mapview instance delegate, simply implement:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
NSLog (@"Callout accessory control tapped");
}
精彩评论