UIButton添加到UIButton没有响应选择器

时间:2013-08-19 12:56:25

标签: ios objective-c uibutton

我有在发布者按钮上添加了一个ui按钮

并实施了选择器方法

但是操作选择器不响应按钮的原因和操作

//To add a discloser button to show route map
UIButton *detailBtn=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinView.leftCalloutAccessoryView=detailBtn;
[detailBtn addTarget:self action:@selector(showRoute:) forControlEvents:UIControlEventTouchUpInside];
DisplayMap *ann=(DisplayMap *)annotation;
detailBtn.tag = ann.detailButtonTag;


UIButton *btnOnPopup=[[UIButton alloc] initWithFrame:CGRectMake(- 207, -6, 300, 43)];
btnOnPopup.backgroundColor = [UIColor whiteColor];
[btnOnPopup addTarget:self action:@selector(showRoute2:) forControlEvents:UIControlEventTouchUpInside];
btnOnPopup.tag = ann.detailButtonTag;
btnOnPopup.userInteractionEnabled = YES;
[detailBtn insertSubview:btnOnPopup aboveSubview:detailBtn];

选择器

[btnOnPopup addTarget:self action:@selector(showRoute2:) forControlEvents:UIControlEventTouchUpInside];

(第二个按钮btnOnPopup)无效。

2 个答案:

答案 0 :(得分:1)

btnOnPopup detailBtn超出了[detailBtn.superview insertSubview:btnOnPopup aboveSubview:detailBtn]; 的界限,所以触动甚至无法实现。无论如何,向按钮添加按钮实际上并不是一个好习惯。将其添加到按钮的超级视图可能会更好一点(并且会使您的触摸工作,但您可能需要调整帧)

{{1}}

答案 1 :(得分:0)

添加视图并点按手势识别器而不是按钮:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureHandler:)];
[singleTap setNumberOfTapsRequired:1];
[view addGestureRecognizer:singleTap];

...

[detailBtn addSubview:view];

...

- (void)tapGestureHandler:(UIGestureRecognizer *)gestureRecognizer
{
    // your code for tap handler is here
}