【问题标题】:UIButton added to UIButton not responding to selectorUIButton 添加到 UIButton 不响应选择器
【发布时间】:2013-08-19 12:56:24
【问题描述】:

我已将 ui 按钮添加到 disclouser 按钮

并实现了选择器方法

但是动作选择器没有响应按钮为什么和做什么

//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)不起作用。

【问题讨论】:

  • 其中有哪些不起作用?
  • 第二个按钮 btnOnPopup
  • 你能发布showRoute2吗:
  • 为什么要添加按钮作为按钮的子视图?内置标注视图不支持大于约 32 像素的附件视图。 不相关的评论:我不建议使用按钮标签来识别注释,因为地图视图提供了更简单、优雅和可靠的方法来做到这一点。有关示例,请参阅 stackoverflow.com/questions/9876042/…stackoverflow.com/questions/9462699/…
  • 保存在“btn”上的三个字母让你的代码阅读和讨论起来很痛苦。

标签: ios objective-c uibutton


【解决方案1】:

您的btnOnPopup 超出了detailBtn 的范围,因此您甚至无法触及。无论如何,向按钮添加按钮并不是一个很好的做法。将它添加到按钮的超级视图可能会更好一些(并且会让你的触摸工作,但你可能需要调整框架)

[detailBtn.superview insertSubview:btnOnPopup aboveSubview:detailBtn];

【讨论】:

    【解决方案2】:

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

    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
    }
    

    【讨论】:

    • 如果视图超出 detailBtn 的范围,仍然无法正常工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    • 2018-06-22
    • 2013-05-03
    • 1970-01-01
    • 2017-01-03
    • 2016-12-14
    相关资源
    最近更新 更多