UIButton无法正常工作。不是第一响应者或隐藏在视图后面

时间:2012-03-16 08:31:03

标签: iphone uiview uibutton

我的UIButton没有回复。我认为它是UIView的背后,或者它不是第一个响应者。我创建了一个自定义视图,其子类是MKAnnotationView。也许这是问题的一部分。我也试过使用userInteractionEnabled,但没有运气。有人有同样的问题吗?

- (id)initWithAnnotation:(id)annotation reuseIdentifier:(NSString *)reuseIdentifier{   
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];

// Create selected and un-selected pin images
_normalPin = [UIImage imageNamed:@"map_pin.png"];
_selectedPin = [UIImage imageNamed:@"selected_map_pin.png"];

// Set current pin to unselected image
self.image = _normalPin;

// cast annotation an our expected BLAnnotation

Annotation *blAnnotation = (Annotation *)annotation;

// create title label and size to text

_titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 280, 20)];
_titleLabel.text = blAnnotation.title;
_titleLabel.textAlignment = UITextAlignmentLeft;
_titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:13];
_titleLabel.textColor = [UIColor whiteColor];
_titleLabel.shadowColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.7];
_titleLabel.shadowOffset = CGSizeMake(0, -1.0);
_titleLabel.backgroundColor = [UIColor clearColor];
_titleLabel.lineBreakMode = UILineBreakModeWordWrap;
_titleLabel.numberOfLines = 3;

CGSize titleLabelSize = [blAnnotation.title sizeWithFont: _titleLabel.font constrainedToSize: CGSizeMake(280, 600) lineBreakMode: _titleLabel.lineBreakMode];
_titleLabel.frame = CGRectMake(_titleLabel.frame.origin.x, _titleLabel.frame.origin.y, 280, titleLabelSize.height);

// create subtitle label and size to text

_subtitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, _titleLabel.frame.size.height + 8, 280, 20)];
_subtitleLabel.text = blAnnotation.subtitle;
_subtitleLabel.textAlignment = UITextAlignmentLeft;
_subtitleLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:11];
_subtitleLabel.textColor = [UIColor whiteColor];
_subtitleLabel.shadowColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.7];
_subtitleLabel.shadowOffset = CGSizeMake(0, -1.0);
_subtitleLabel.backgroundColor = [UIColor clearColor];
_subtitleLabel.lineBreakMode = UILineBreakModeWordWrap;
_subtitleLabel.numberOfLines = 4;

CGSize subtitleLabelSize = [blAnnotation.subtitle sizeWithFont: _subtitleLabel.font constrainedToSize: CGSizeMake(235, 600) lineBreakMode: _subtitleLabel.lineBreakMode];
_subtitleLabel.frame = CGRectMake(_subtitleLabel.frame.origin.x, _subtitleLabel.frame.origin.y, subtitleLabelSize.width, subtitleLabelSize.height);

// create custom button
_infoButton = [UIButton buttonWithType:UIButtonTypeCustom];
_infoButton.frame = CGRectMake(0, _titleLabel.frame.size.height + _subtitleLabel.frame.size.height + 80, 175, 50);
//_infoButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//_infoButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

//[_infoButton setImage:[UIImage imageNamed:@"more_info.png"] forState:UIControlStateNormal];
[_infoButton addTarget:self action:@selector(pressButton:) forControlEvents:UIControlEventTouchUpInside];

// create callout view to be shown once a annotation view is selected

_calloutView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, _titleLabel.frame.size.height + _subtitleLabel.frame.size.height + _infoButton.frame.size.height + 5)];
_calloutView.hidden = YES;
_calloutView.userInteractionEnabled = YES;
[self addSubview:_calloutView];

// create rounded rect background and size to text

UIImageView *backgroundRect = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, _calloutView.frame.size.width, _calloutView.frame.size.height)];
//[backgroundRect setAlpha:0.8];
//[backgroundRect setOpaque:NO];
[backgroundRect.image drawInRect:_calloutView.frame];

UIGraphicsBeginImageContext(backgroundRect.frame.size);

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect frame = _calloutView.bounds;
[[UIColor colorWithRed:.2 green:.2 blue:.2 alpha:1] set];
CGPathRef roundedRectPath = [self roundedRectPath:frame radius:5];
CGContextAddPath(ctx, roundedRectPath);
CGContextFillPath(ctx);
CGPathRelease(roundedRectPath); 

backgroundRect.userInteractionEnabled = YES;
backgroundRect.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

[_calloutView addSubview:backgroundRect];
[_calloutView addSubview:_titleLabel];
[_calloutView addSubview:_subtitleLabel];
[_calloutView addSubview:_infoButton];

// position callout above pin

_calloutView.frame = CGRectMake(-140, -_calloutView.frame.size.height+10, _calloutView.frame.size.width, _calloutView.frame.size.height);

backgroundRect = nil;

return self;

}

2 个答案:

答案 0 :(得分:0)

尝试使用[self addSubview:_calloutView] 之后将您的按钮和标签添加到* callout View *作为子视图,而不是之前。

让我知道结果如何

答案 1 :(得分:0)

我的猜测是你的按钮指向自己作为目标。如果按钮不是第一个响应者并且它被点击,则在上面的场景中不会调用其目标函数。我通过使用lazy var在Swift 4 Xcode 9中声明UIButton来修复我的类似问题,将目标指向父视图/视图控制器。

// Sets target to parent view or view controller
// someFunction is called even if the button is not the first responder when tapped
lazy var nextCreateButton: UIButton = {
    let button = UIButton(type: .system)
    button.addTarget(self, action: #selector(someFunction), for: .touchUpInside)
    return button
}()

// Sets target to the button itself
// someFunction is not called when the button is not the first responder
let nextCreateButton: UIButton = {
    let button = UIButton(type: .system)
    button.addTarget(self, action: #selector(someFunction), for: .touchUpInside)
    return button
}()