質問

Button on the calloutView

the structure of my calloutView

The problem I encountered: I click on the button, but it seems that the CalloutView received my clicking instead of the UIButton.

Some explanation first: 1.the "SYSUMyAnnoCalloutView" is a MKPinAnnotationView 2.the "SYSUMyCalloutImageView" is a UIImageView, which I init it and addSubview on the "SYSUMyAnnoCalloutView" 3. the UIButton and the UILabel was addSubviewed in the "SYSUMyCalloutImageView"

Corresponding code: 1. In the "SYSUMyAnnoCalloutView.m"

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
if (selected){
    [self.calloutImageView setFrame:CGRectMake(-75, -100, 230, 130)];

    [self animateCalloutAppearance];
    //[self.calloutImageView becomeFirstResponder];
    //[self.calloutImageView.detailButton addTarget:self.calloutImageView action:@selector(clickForDetail) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.calloutImageView];
    [self setCanShowCallout:NO];
}
else
{
    //Remove your custom view...
    [self.calloutImageView removeFromSuperview];
}
}
  1. In the "SYSUMyCalloutImageView.m"

    - (id)initWithImage:(UIImage *)image
    {
    self = [super initWithImage:image];
    if (self){
        //Label
    CGRect boundsForLabel;
    boundsForLabel.origin.x = 20;
    boundsForLabel.origin.y = 50;
    boundsForLabel.size.width = self.bounds.size.width - 100;
    boundsForLabel.size.height = 20;
    self.messageLabel = [[UILabel alloc] initWithFrame:boundsForLabel];
    self.messageLabel.backgroundColor = [UIColor clearColor];
    [self addSubview:self.messageLabel];
    //detailButton
    CGRect buttonRect;
    buttonRect.origin.x = 20;
    buttonRect.origin.y = 80;
    buttonRect.size.width = boundsForLabel.size.width-50;
    buttonRect.size.height = 30;
    self.detailButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.detailButton setFrame:buttonRect];
    [self.detailButton setTitle:@"Click for detail" forState:UIControlStateNormal];
    self.detailButton.userInteractionEnabled = YES;
    //[self.detailButton addTarget:self.superview action:@selector(clickForDetail) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.detailButton];
    

    } return self; }

could anybody solve my problem?? will be really appreciate that:)

役に立ちましたか?

解決

by default an UIImageView do not accept user interactions.

You should try adding this line of code in the initWithImage of your SYSUMyCalloutImageView.m

[self setUserInteractionEnabled:YES];
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top