ios - Annotation的calloutView上UIButton点击失败

标签 ios annotations uibutton mkmapview uiresponder

Button on the calloutView

the structure of my calloutView

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

首先进行一些解释:1.“SYSUMyAnnoCalloutView”是一个 MKPinAnnotationView 2.“SYSUMyCalloutImageView”是一个 UIImageView,我将其初始化并在“SYSUMyAnnoCalloutView”上 addSubview 3. UIButton 和 UILabel 在“SYSUMyCalloutImageView”中 addSubviewed ”

对应代码: 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];
    

    } 返回 self ; }

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

最佳答案

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

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

[self setUserInteractionEnabled:YES];

关于ios - Annotation的calloutView上UIButton点击失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13926864/

相关文章:

ios - 目标为 :self not being sent to self? 的 UIButton

ios - 在 Button Swift 中通过标签发送行和部分

ios - 打开 segue 时无法识别的实例选择器

java - 如何使用 @Value Spring 注解注入(inject) Map?

java - 如何在 AspectJ 中将方法与带注释的参数匹配

objective-c - UIButton 不可点击

ios - 如何在 Swift 的 UIButton 中删除图像和写入文本?

ios - 触发UIApplicationWillTerminateNotification以“停止” Xcode操作

objective-c - 使用 Accelerate Framework 的 iOS 的 FFT 音调检测?

java - 实际中注释扫描采用什么策略?