2012-03-16 22 views
0

我的UIButton沒有響應。我認爲它在UIView背後,或者它不是第一響應者。我做了一個自定義的視圖,其子類是MKAnnotationView。也許這是問題的一部分。我也嘗試使用userInteractionEnabled,但沒有運氣。任何人都有同樣的問題?UIButton無法正常工作。不是第一響應者或隱藏在視圖

- (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; 

}

回答

0

嘗試使用[self addSubview:_calloutView]後您添加按鈕,您的標貼,以* _calloutView *爲子視圖,而不是之前。

讓我知道結果如何

+0

感謝您的回答,但是這並沒有解決問題。 – Ryan 2012-03-16 13:19:42

+0

好吧,那麼你會解釋一下你的按鈕沒有響應嗎?它是不可觸摸的,你能在屏幕上看到它嗎? – 2012-03-18 19:06:43

+0

我可以在屏幕上看到它,但它不可觸摸。所以我認爲它隱藏在某種東西背後。 – Ryan 2012-03-19 07:48:10