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