【问题标题】:UIButton fail to be clicked on the calloutView of the AnnotationUIButton在Annotation的calloutView上点击失败
【发布时间】:2012-12-18 05:32:03
【问题描述】:

我遇到的问题:我点击了按钮,但似乎是 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];
    

    } 回归自我; }

谁能解决我的问题??将不胜感激:)

【问题讨论】:

    标签: ios annotations uibutton mkmapview uiresponder


    【解决方案1】:

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

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

    [self setUserInteractionEnabled:YES];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-13
      • 1970-01-01
      • 1970-01-01
      • 2014-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多