0

calloutView 上的按钮

我的 calloutView 的结构

我遇到的问题:我点击了按钮,但似乎是 CalloutView 收到了我的点击,而不是 UIButton。

首先解释一下: 1.“SYSUMyAnnoCalloutView”是一个 MKPinAnnotationView 2.“SYSUMyCalloutImageView”是一个 UIImageView,我将它初始化并在“SYSUMyAnnoCalloutView”上添加Subview 3. UIButton 和 UILabel 在“SYSUMyCalloutImageView”中添加Subviewed

对应代码: 1. 在“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. 在“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];
    

    } 返回自我;}

有人可以解决我的问题吗?将非常感激:)

4

1 回答 1

1

默认情况下 UIImageView 不接受用户交互。

initWithImage您应该尝试在SYSUMyCalloutImageView.m中添加这行代码

[self setUserInteractionEnabled:YES];
于 2012-12-18T05:45:08.877 回答